Reputation: 1
I am using horizontal LinearLayoutManager
to create a horizontal list on my RecyclerView
as shown in the picture below
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.
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
Reputation: 967
Disabling recycler is not a proper solution here. do follow following steps:
Add one boolean flag in HomeProduitsPromoInnerItem
class. let's say isShowingTitle
.
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
Reputation: 76
HomeProduitsPromoInnerItem
data class, that will save the title visible state of the specific item.onBindViewHolder
based on the flag value.Upvotes: 0