0

I am trying to select the value from Recyclerview highlight the selected item. I want to change the bg color and text color and image. Now I am using the following code.

selector_row:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/darker_gray" android:state_pressed="false" android:state_selected="true" />
    <item android:drawable="@color/store_search_list_item_background"/>
</selector>

In recyclerview on onBindViewHolder :

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectedItems.get(position, false)) {
                    selectedItems.delete(position);
                    holder.itemView.setSelected(false);
                }
                else {
                    selectedItems.put(position, true);
                    holder.itemView.setSelected(true);
                }
            }
        });

Now I can highlight the selected row. My problem is 1) When I select another row it's also getting highlighted. I want to highlight only one row. If user select another row previous row should not be highlighted.

2) How to change the text and image when highlight the row.

Please let me any idea to resolve this two problems.

Vijay
  • 3,152
  • 3
  • 24
  • 33

2 Answers2

2

You can save click position in a variable so whenever new click happens you simply replace your variable value with new clicked position and after that don't forget to use notifyItemChanged() ; Now its very simple in your bindview check for your selected position and change the background of item.

rough ex.

  onClick(){
    highlightedPosition = getLayoutPostion();
    notifyItemChanged();
    }

  onBindView(int position){
    if(highlightedPosition == position){
    higlightedBackground();
    }else{
    normalBackground();
    }
Pushpendra
  • 2,791
  • 4
  • 26
  • 49
0

android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" android:foreground="@drawable/xx_sel"
or
android:foreground="@drawable/xx_bg_sel" I use it to let my album photo item pregress or fouce can has a mask on it.
May it will help you.

LeeYaochi
  • 41
  • 5