Tony J Huang Tony J Huang - 23 days ago 18
Android Question

Hiding views in RecyclerView

I have code like this

public static class MyViewHolder extends RecyclerView.ViewHolder {
@InjectView(R.id.text)
TextView label;

public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.inject(this, itemView);
}

public void hide(boolean hide) {
label.setVisibility(hide ? View.GONE : View.VISIBLE);
}
}


which maps to a single row in a
RecyclerView
.
R.id.text
is in fact the root view of the layout that gets inflated and passed in to the constructor here.

I'm using the default implementation of
LinearLayoutManager
.

In
bindViewHolder
, I call
hide(true)
on an instance of
MyViewHolder
, but instead of collapsing the row as expected, the row becomes invisible, maintaining its height and position in the
RecyclerView
. Has anyone else run into this issue?

How do you hide items in a RecyclerView?

Answer

There is no built in way to hide a child in RV but of course if its height becomes 0, it won't be visible :). I assume your root layout does have some min height (or exact height) that makes it still take space even though it is GONE.

Also, if you want to remove a view, remove it from the adapter, don't hide it. Is there a reason why you want to hide instead of remove ?

Comments