【问题标题】:RecyclerView changing the viewRecyclerView 改变视图
【发布时间】:2016-11-14 15:59:36
【问题描述】:

我试过thisholder.setIsRecyclable(false);

但我无法保持视图的背景

这是我选择后的看法

当我滚动它时,它会失去视图,

任何指导都会很棒

编辑:代码

    holder.setIsRecyclable(false);

    final GradientDrawable gd2=new GradientDrawable();
    gd2.setShape(GradientDrawable.OVAL);
    gd2.setSize(24,24);
    gd2.setStroke(2, Color.parseColor("#5FB382"));
    final GradientDrawable gd = new GradientDrawable();
    gd.setShape(GradientDrawable.OVAL);
    gd.setStroke(2, Color.parseColor("#FBAA35"));
    gd.setSize(24,24);

    holder.favorite.setTextColor(Color.parseColor("#FBAA35"));
    holder.hatIcon.setTextColor(Color.parseColor("#5FB382"));
    holder.hatIcon.getCurrentTextColor();
    holder.hatIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (holder.hatIcon.getCurrentTextColor() == Color.parseColor("#5FB382")) {

                    holder.hatIcon.setTextColor(Color.parseColor("#ffffff"));
               holder.hatIcon.setBackgroundColor(Color.parseColor("#5FB382"));
                gd2.setColor(Color.parseColor("#5FB382"));
            }
            else {
                Debug.e();
                holder.hatIcon.setTextColor(Color.parseColor("#5FB382"));
                gd2.setColor(Color.parseColor("#ffffff"));
            }
        }
    });

【问题讨论】:

  • @MarcinKunert 更新了代码

标签: android android-recyclerview


【解决方案1】:

通过数据模型支持您的视图,或使用标志,以便在重新使用视图时回收不会更改列表的显示。

您也可以通过向持有者类添加一个布尔值来做到这一点,如下所示:

class Holder extends RecyclerView.ViewHolder{
    boolean isSelected = false;
    //... other view declaration and constructor
}

然后使用这个标志来确定选择状态:

public void onBindViewHolder(Holder holder, int position) {

  // check if holder.isSelected

  if(holder.isSelected){

      // Selected state
      holder.hatIcon.setTextColor(Color.parseColor("#5FB382"));

  }else{
      // non- selected state
       holder.hatIcon.setTextColor(Color.parseColor("#ffffff"));
       holder.hatIcon.setBackgroundColor(Color.parseColor("#5FB382"));
  }

 // listener for the selection state change
 holder.hatIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (holder.isSelected) {

                holder.isSelected = false;
                    holder.hatIcon.setTextColor(Color.parseColor("#ffffff"));
               holder.hatIcon.setBackgroundColor(Color.parseColor("#5FB382"));
                gd2.setColor(Color.parseColor("#5FB382"));
            }
            else {
                holder.isSelected = true;
                Debug.e();
                holder.hatIcon.setTextColor(Color.parseColor("#5FB382"));
                gd2.setColor(Color.parseColor("#ffffff"));
            }
        }
    });
}

【讨论】:

  • 为什么holder.isSelected false
  • @rookieDeveloper 很好,那是复制和粘贴错误,第二个应该是真的,我已经解决了这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-21
  • 1970-01-01
  • 2021-10-28
  • 2019-04-30
  • 2018-01-14
  • 2018-12-21
  • 1970-01-01
相关资源
最近更新 更多