AleX CoSTa
AleX CoSTa

Reputation: 83

Change style of RecyclerView item onClick

I would want to change the color of an item in RecyclerView when I click on it. But I want it to remain coloured even after the click.. until I click on an another item (that becomes coloured).

In the onBindViewHolder method of the RecyclerViewAdapter I have tried this:

     holder.linearlayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            holder.linearlayout.setBackgroundColor(Color.RED);
        }
     }

The problem is that if I try to set the color in the onBindViewHolder method, the item remains coloured even after I click on an another item, because in the onBindViewHolder method I can see only the actual item.

Is there a way to set all items to the original state (no item coloured) and set the color only to the last item clicked?

Upvotes: 1

Views: 3159

Answers (3)

Beyazid
Beyazid

Reputation: 1835

I created sample RecyclerView adapter. You can replace TextView with your LinearLayout. You should use setBackgroundColor() instead of setTextColor()

/**
 * Created by beyazid on 11.03.2019.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

private Context context;
private LayoutInflater inflater;
private ArrayList<String> list;
private int indexOfColoredItem = -1;

public MyAdapter(Context context, ArrayList<String> list) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;
    this.list = list;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.my_row_for_recycler_view, parent, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.bindItem(position);
    holder.tvDummy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            indexOfColoredItem = position;
            notifyDataSetChanged();
        }
    });
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView tvDummy;
    public MyViewHolder(View itemView) {
        super(itemView);
        tvDummy = itemView.findViewById(R.id.text);
    }
    void bindItem(int pos) {
        String txt = list.get(pos);
        tvDummy.setText(txt);
        if(indexOfColoredItem==pos){
            tvDummy.setTextColor(ContextCompat.getColor(context, R.color.selectedColor));
        } else{
            tvDummy.setTextColor(ContextCompat.getColor(context, R.color.yourDefaulColor));
        }
    }
}
}

Upvotes: 0

Rajat Mittal
Rajat Mittal

Reputation: 423

It is simple, Just declare one global variable

int mPreviousIndex = -1

then inside your onclick

holder.linearlayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
          mPreviousInde = position           //postition- Position of Adapter
    }
 }

after this, write this in your onBindViewHolder

if(mPreviousIndex==position){
     holder.linearlayout.setBackgroundColor(Color.RED);    //color on item selecting item
}
else{
     holder.linearlayout.setBackgroundColor(Color.WHITE);    //color on item unselecting item
}

Upvotes: 1

ror
ror

Reputation: 3500

It is very simple. Basically, I would suggest you to:

  1. Keep index of your last clicked item in your adapter.
  2. Do your coloring only inside onBindViewHolder (and not inside your onClickListener) based on index (say if index has some default value like -1, you don't color, if it has 0..count, you do the coloring)
  3. Create method in your adapter that a) stores last clicked index in variable b) updates index with new value c) calls notifyItemChanged for old index d) calls notifyItemChanged for new index
  4. Call newly created method under your onClickListener.

Upvotes: 0

Related Questions