-2

I have tried this and also holder.setIsRecyclable(false);

But i am unable to maintain the background of view

this is my view after selectingenter image description here

and when I scroll it loses the view, enter image description here

any guidance will be great

EDIT : CODE

    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"));
            }
        }
    });
Community
  • 1
  • 1
rookieDeveloper
  • 2,459
  • 1
  • 22
  • 44

1 Answers1

2

Back your view by a data model, or use a flag so recycling doesn't change the display of the list when views are re-used.

You can also do this by adding a boolean value to your holder class like so:

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

then use this flag to determine the selection state:

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"));
            }
        }
    });
}
kandroidj
  • 13,784
  • 5
  • 64
  • 76