1

I am trying to change the background color of my recyclerview row when it is clicked. When one row is clicked, its background color is changed. If another row is clicked, then the previous row changes back to its old state and the newly clicked one changes color.

I've tried achieving this using selectors, but when i release the touch, the color just goes back to normal and none of my rows are highlighted.
Would anyone have an idea how to approach this? Thanks in advance

Here is my selector code - Desired goal (Selected row STAYS charcoal until another row is selected)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true"
        android:state_pressed="true" android:drawable="@color/charcoal_dark" />
    <item android:state_enabled="true"
        android:state_focused="false" android:drawable="@color/charcoal_dark" />
    <item android:drawable="@color/black"/>
</selector>
DanielRM
  • 636
  • 7
  • 24

2 Answers2

1

There are several ways this is a question I posted about multiselection. But your rows shall have a boolean property, for me this is the most simple way to do it of all I have found. In my question @cool alien showed a different aproach. Here is another one, the only difference from your question is that you are looking for single-select. For Single select in your OnBindViewHolder, you should have a variable tracking previous selections and adjust, when there is a click. I have at the end of my question my code.

Guanaco Devs
  • 1,822
  • 2
  • 21
  • 38
  • Thank you. I will try this solution – DanielRM Jul 06 '17 at 19:54
  • @DanielRM I've uploaded a video here https://www.dropbox.com/s/z27y7hhutgh8d5o/ScreenRecord_2017-07-06-14-48-20.mp4?dl=0 to show you my implementation. Sure there are more elegant and technical ways, but being a newbie, I feel that this works perfect. – Guanaco Devs Jul 06 '17 at 20:53
  • Thank you!! this is exactly what i need. – DanielRM Jul 06 '17 at 22:02
0

Have you tried handling onClick event of the element and changing the background color property?

Here is an example code from post How to Change color of Button in Android when Clicked?

boolean check = false;
Button backward_img;
Button backward_img1;
backward_img = (Button) findViewById(R.id.bars_footer_backward);
backward_img1 = (Button) findViewById(R.id.bars_footer_backward1);
backward_img.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        check = true;
        backward_img.setBackgroundColor(Color.BLUE);
    }
});

if (check == true) {
    backward_img1.setBackgroundColor(Color.RED);
    backward_img.setBackgroundColor(Color.BLUE);
}
Amit Mahajan
  • 895
  • 6
  • 34
  • Thanks for the tip, reading this code, im not quite sure i see where the previously selected row will go back to its original color when the new row is selected, perhaps im not seeing it? – DanielRM Jul 05 '17 at 20:48
  • This happened because you use selector i guess, perhaps when the focus of that element is gone the selection is back to previous state. You can toggle the color for other elements using some state variable (example the check variable). – Amit Mahajan Jul 05 '17 at 21:00