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

huangapple go评论123阅读模式
英文:

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

问题

我正在使用水平的LinearLayoutManager在我的RecyclerView上创建一个水平列表,如下图所示:
How to hide only one clicked item title from a horizontal LinearLayoutManager based RecyclerView

我的目的是在单击RecyclerView项目标题时隐藏它。但是,例如,当我单击第一个项目标题以隐藏它时,它成功隐藏,但是位置在8、15、22、29等的项目标题也被隐藏,如下图所示:
How to hide only one clicked item title from a horizontal LinearLayoutManager based RecyclerView
How to hide only one clicked item title from a horizontal LinearLayoutManager based RecyclerView

我的适配器:

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();
    }
}

我的片段代码:

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));
英文:

I am using horizontal LinearLayoutManager to create a horizontal list on my RecyclerView as shown in the picture below
How to hide only one clicked item title from a horizontal LinearLayoutManager based RecyclerView

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.
How to hide only one clicked item title from a horizontal LinearLayoutManager based RecyclerView
How to hide only one clicked item title from a horizontal LinearLayoutManager based RecyclerView

My adapter:

public class HomeProduitsPromoAdapter extends RecyclerView.Adapter&lt;HomeProduitsPromoAdapter.MyViewHolder&gt;{
private List&lt;HomeProduitsPromoInnerItem&gt; dataSet;
private Context mContext;
public HomeProduitsPromoAdapter(Context context, List&lt;HomeProduitsPromoInnerItem&gt; 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));

答案1

得分: 1

禁用回收器不是合适的解决方案。请按照以下步骤操作:

  1. HomeProduitsPromoInnerItem 类中添加一个布尔标志,命名为 isShowingTitle
  2. 现在,在您的适配器的 onBindViewHolder() 方法中添加以下条件来显示/隐藏项目标题(将您的 holder.nomProduit.setOnClickListener... 行替换为以下内容):
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);
}

使用这种方式,每个项目的标题可见性将存储在 dataSet 列表中。每当 recyclerView 重新绘制项目时,您将获得更新的标题可见性。

在您的问题中,我没有看到您如何使标题可见。因此,我没有在答案中添加那部分内容。

英文:

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 -&gt; {
    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.

答案2

得分: 0

  1. 在你的 HomeProduitsPromoInnerItem 数据类内部添加一个标志,用于保存特定项目的标题可见状态。
  2. 为项目的标题添加一个监听器,使用 setOnClickListener 处理点击事件(看起来你已经这样做了)。
  3. 强制刷新 RecyclerView。
  4. 根据标志值在 onBindViewHolder 中处理项目/标题的可见性。
英文:
  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.

huangapple
  • 本文由 发表于 2023年8月10日 16:29:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定