How to hide an item from Recycler View on a particular condition?

本秂侑毒 提交于 2019-11-26 20:45:19

You should hide all views or parent from UsersViewholder layout xml.

view.setVisibility(View.GONE);

But don't forget to set them VISIBLE otherwise, you will end up with some strange things from recycling

incognito

In some cases changing only visibility attribute might still end up as allocated blank space (because of parent view's padding, margins, inner elements etc). Then changing height of the parent view helps:

holder.itemView.setVisibility(View.GONE); 
holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));

Then be sure that in the condition that it should be visible, also set:

holder.itemView.setVisibility(View.VISIBLE);
holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

You need to do that because the viewHolder is recycled as you scroll, if you change properties as this and never return them to their natural state, other elements will be already hidden in the event they reuse the same view.

IF

view.setVisibility(View.GONE);

gives you a Blank view

Then follow This.

public static class Data_ViewHolder extends RecyclerView.ViewHolder {
    private final LinearLayout layout;
    final LinearLayout.LayoutParams params;

    public Show_Chat_ViewHolder(final View itemView) {
        super(itemView);
        .
        .
        .
        layout =(LinearLayout)itemView.findViewById(R.id.show_item_layout);
        params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT);
        .
        .
        .
    }

    private void Layout_hide() {
        params.height = 0;
        //itemView.setLayoutParams(params); //This One.
        layout.setLayoutParams(params);   //Or This one.

    }
  }

Now Call from Adapter

mFirebaseAdapter = new FirebaseRecyclerAdapte......{
public void populateViewHolder.....{

if(model.getData().equals("..Something.."))
  {
      viewHolder.Layout_hide();
  }
else
      viewHolder.Person_Email(model.getEmail());
   }
 }

There is no built in way to hide a child in RecyclerView. But you can implement this feature in your Adapter.

public class MyAdapter extends RecyclerView.Adapter<...>{
    List<Object> items;
    Map<Integer,Object> deletedItems;
    ...

    public void hideItem(final int position) {
         deletedItems.add(position, items.get(position));
         items.remove(position);
         notifyItemRemoved(position);
    }

    ....
}
public class OfferViewHolder extends RecyclerView.ViewHolder {
    public TextView textViewOfferName;
    public LabelImageView labelImageView;
    public TextView textViewOldPrice;
    public TextView textViewNewPrice;
    public TextView textViewShopName;
    public TextView textViewTimeDate;
    public TextView textViewDistance;

    public LinearLayout linearLayoutMain;


    public OfferViewHolder(View view) {
        super(view);
        linearLayoutMain=(LinearLayout) view.findViewById(R.id.ll_main);
        textViewOfferName = (TextView) view.findViewById(R.id.textViewoffername);
        labelImageView=(LabelImageView) view.findViewById(R.id.labelImageView) ;
        textViewOldPrice=(TextView) view.findViewById(R.id.textViewOldPrice);
        textViewNewPrice=(TextView) view.findViewById(R.id.textViewNewPrice);
        textViewShopName=(TextView) view.findViewById(R.id.textViewShopName);
        textViewTimeDate=(TextView) view.findViewById(R.id.textViewDate);
        textViewDistance=(TextView) view.findViewById(R.id.textViewDistance);

        linearLayoutMain.setVisibility(View.GONE);
        textViewOfferName.setVisibility(View.GONE);
        labelImageView.setVisibility(View.GONE);
        textViewOldPrice.setVisibility(View.GONE);
        textViewNewPrice.setVisibility(View.GONE);
        textViewShopName.setVisibility(View.GONE);
        textViewTimeDate.setVisibility(View.GONE);
        textViewDistance.setVisibility(View.GONE);



    }


}`enter code here`

THEN IN YOUR ADAPTER

 if (a.equals(offer.getOfferCategory())) {


                        if (offer.getOfferCategory()==null){
//                            chatMessageViewHolder.getLinearLayoutMain().setVisibility(View.GONE);
//                            chatMessageViewHolder.linearLayoutMain.setLayoutParams(new RecyclerView.LayoutParams(0, 0));


                        }
                        else {
                            chatMessageViewHolder.itemView.setVisibility(View.VISIBLE);
                            chatMessageViewHolder.textViewShopName.setText(offer.getOfferCategory());
                            chatMessageViewHolder.linearLayoutMain.setVisibility(View.VISIBLE);
                            chatMessageViewHolder.textViewOfferName.setVisibility(View.VISIBLE);
                            chatMessageViewHolder.labelImageView.setVisibility(View.VISIBLE);
                            chatMessageViewHolder.textViewOldPrice.setVisibility(View.VISIBLE);
                            chatMessageViewHolder.textViewNewPrice.setVisibility(View.VISIBLE);
                            chatMessageViewHolder.textViewShopName.setVisibility(View.VISIBLE);
                            chatMessageViewHolder.textViewTimeDate.setVisibility(View.VISIBLE);
                            chatMessageViewHolder.textViewDistance.setVisibility(View.VISIBLE);

                        }

Thank you lorescu George Cătălin and Dhalav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!