Skip to content Skip to sidebar Skip to footer

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) {

Solution 1:

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 ?

Solution 2:

Put method setVisibility(boolean isVisible) in ViewHolder.

You can change itemView params(width and height) for LayoutManager:

publicstaticclassViewHolderextendsRecyclerView.ViewHolder implementsView.OnClickListener{
    ...

    publicvoidsetVisibility(boolean isVisible){
        RecyclerView.LayoutParamsparam= (RecyclerView.LayoutParams)itemView.getLayoutParams();
        if (isVisible){
            param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            param.width = LinearLayout.LayoutParams.MATCH_PARENT;
            itemView.setVisibility(View.VISIBLE);
        }else{
            itemView.setVisibility(View.GONE);
            param.height = 0;
            param.width = 0;
        }
        itemView.setLayoutParams(param);
    }

    publicViewHolder(View itemView) {
        super(itemView);
        ...
    }
}

and change visibility for ItemDecoration (Divider):

publicclassDividerItemDecorationextendsRecyclerView.ItemDecoration {
    ...

    @OverridepublicvoidonDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        ...

        for (inti=0; i < parent.getChildCount(); i++) {

            if (parent.getChildAt(i).getVisibility() == View.GONE) 
                continue;

            /* draw dividers */    

        }
    }
}

Solution 3:

You CAN do it!

First, you need to detect which position of item that you want to hide. You can custom getItemViewType to do it.

Next, on onCreateViewHolder, depend on the view type. You can do something like this:

if(viewType == TYPE_HIDE) {
                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_item, parent, false);
                vHolder = new ViewHolder(context, v, viewType, this);
                break;
        }
        return vHolder; 

-> empty item is a layout that have nothing, (in other word, it is default layout whenever created). or code:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"></LinearLayout>

Hope it help!

Solution 4:

Okay, so the way I did it in the end was I had my whole dataset, say, myObjects and I had scenarios where I would only want to show subsets of that dataset.

Since setting visibility of rows in RecyclerView doesn't cause the heights to collapse, and setting the heights of the rows did not appear to do anything either, what I had to do was just keep a secondary dataset called myObjectsShown which was nothing more than a List<Integer> that would index into myObjects to determine which objects would be displayed.

I would then intermittently update myObjectsShown to contain the correct indices.

Therefore,

publicintgetItemCount() {
    return myObjectsShown.size();
}

and

publicvoidonBindViewHolder(MyViewHolder holder, int position) {
    Object myObject = myObjects.get(myObjectsShown.get(position));
    // bind object to viewholder here...
} 

Solution 5:

For hiding view in RecyclerView I hide/show view in OnBindViewHolder:

if (item.isShown) {
    vh.FooterLayout.setVisibility(View.Visible);
} else {
    vh.FooterLayout.setVisibility(View.Gone);
}

And for example - from activity I simply redraw needed item: _postListAdapter.notifyItemChanged(position)// if you want show/hide footer - position is amountOfPosts.size() and also change bool variable - amountOfPosts[amountOfPosts.size()].isShown

Post a Comment for "Hiding Views In Recyclerview"