Tony J Huang Tony J Huang - 1 year ago 105
Android Question

Hiding views in RecyclerView

I have code like this

public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView label;

public MyViewHolder(View 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
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

, I call
on an instance of
, but instead of collapsing the row as expected, the row becomes invisible, maintaining its height and position in the
. Has anyone else run into this issue?

How do you hide items in a RecyclerView?

Answer Source

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 ?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download