Change the color of the Recycleview line

Asked

Viewed 246 times

0

have a RecycleView and would like to change the color of the line when selected, click the selection I have already implemented and working.

1 answer

0

In the Adapter of his RecycleView create a variable to control the position it has selected in the list and initially set the value -1.

In the onBindViewHolder define the onClicklistner to update the position in the variable and call the notifyDataSetChanged. Still in the onBindViewHolder also put the if to check the position of the variable with the position of the item being updated and change the colors.

With the call of notifyDataSetchangeg() the onBindViewHolder is re-examined, with this going through again if for deciding the color of the items, changing the color of the previously selected item and the currently selected item.

Example:

public void onBindViewHolder(final ViewHolder holder, final int position) {    
    holder.row_linearlayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            row_index=position;
            notifyDataSetChanged();
        }
    });
    if(row_index==position){
        holder.row_linearlayout.setBackgroundColor(Color.parseColor("#567845"));
    }
    else
    {
        holder.row_linearlayout.setBackgroundColor(Color.parseColor("#ffffff"));
    }    
}
  • I put to change the color of Textview, changed the color blz, but the click on the text does not work, after it click is ok.

  • I don’t get it, I could explain it better ?

  • In my Cardview I have only one Textview, in my Adapter I have taken the click that returns the position of Recycle. What v showed worked, but when it changes color I can no longer receive the position of Recycle, Got it?

  • Sorry, I couldn’t understand yet, add in your question your code and what is happening, in more detail...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.