Landry Joe
Landry Joe

Reputation: 1

How to hide only one clicked item title from a horizontal LinearLayoutManager based RecyclerView

I am using horizontal LinearLayoutManager to create a horizontal list on my RecyclerView as shown in the picture below Screenshot 1

My purpose is to hide the RecyclerView item title when I click on it. But when I click for example on the first item title to hide it, it is successfully hidden but the titles of items at positions 8,15,22,29, etc are also hidden as shown in the picture below. Picture 2 Picture 3

My adapter:

public class HomeProduitsPromoAdapter extends RecyclerView.Adapter<HomeProduitsPromoAdapter.MyViewHolder>{

    
    private List<HomeProduitsPromoInnerItem> dataSet;
    private Context mContext;

    public HomeProduitsPromoAdapter(Context context, List<HomeProduitsPromoInnerItem> listItems) {
        this.dataSet = listItems;
        this.mContext = context;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        protected TextView nomProduit,prixProduit,prixBarre;
        protected ImageView itemImage;

        public MyViewHolder(View view) {
            super(view);

            this.nomProduit = (TextView) view.findViewById(R.id.textGoodsName);
            this.prixProduit = (TextView) view.findViewById(R.id.textGoodsPrice);
            this.prixBarre = (TextView) view.findViewById(R.id.textGoodsOldPrice);
            this.itemImage = (ImageView) view.findViewById(R.id.imageGoodsPic);

        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_home_produits_promo_inner_content, parent, false);

        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int i) {

        final HomeProduitsPromoInnerItem singleItem = dataSet.get(i);

        holder.nomProduit.setText(singleItem.getNomProduitWithUnite());
        holder.prixProduit.setText(singleItem.getPrixProduit());
        holder.prixBarre.setText(singleItem.getPrixBarre());
        holder.prixBarre.setPaintFlags(holder.prixBarre.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);

        int placeholder=R.drawable.ic_category_def;
        Glide.with(holder.itemView)
                .load(singleItem.getUrlImage())
                .placeholder(placeholder)
                .fitCenter()
                .into(holder.itemImage);

        holder.nomProduit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                holder.nomProduit.setVisibility(View.INVISIBLE);
            }
        });

    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }
}

My fragment code

private HomeProduitsPromoAdapter homeMeilleursProduitsAdapter;

homeMeilleursProduitsAdapter=new HomeProduitsPromoAdapter(getContext(), homeProduitsPromoInnerItems);
home_recyclerview_meilleurs_produits.setAdapter(homeMeilleursProduitsAdapter);
home_recyclerview_meilleurs_produits.setLayoutManager(new LinearLayoutManager(getContext(),RecyclerView.HORIZONTAL,false));

Upvotes: 0

Views: 37

Answers (2)

Nilesh B
Nilesh B

Reputation: 967

Disabling recycler is not a proper solution here. do follow following steps:

  1. Add one boolean flag in HomeProduitsPromoInnerItem class. let's say isShowingTitle.

  2. Now in your adapter's onBindViewHolder() method add following condition to show/hide item title (replace your holder.nomProduit.setOnClickListener... line with following):

    if (singleItem.isShowingTitle) {
        holder.nomProduit.setVisibility(View.VISIBLE);
        holder.nomProduit.setOnClickListener((OnClickListener) view -> {
            singleItem.isShowingTitle = false;
            holder.nomProduit.setVisibility(View.INVISIBLE);
        });
    
    } else {
        holder.nomProduit.setVisibility(View.INVISIBLE);
        holder.nomProduit.setOnClickListener(null);
    }
    

using this, every item's title visibility will be stored in dataSet list. and every time when recyclerView redraw the item, you will get updated title visibility.

In your question, i don't see how you going to make title visible. SO i'm not adding that part in answer.

Upvotes: 1

weaklin
weaklin

Reputation: 76

  1. Include a flag inside your HomeProduitsPromoInnerItem data class, that will save the title visible state of the specific item.
  2. Add a listener to handle click on the title with a setOnClickListener on your item (seems you already do that).
  3. Force a RecyclerView refresh.
  4. Handle the visibility of the item/title inside the onBindViewHolder based on the flag value.

Upvotes: 0

Related Questions