2

I got a RecyclerView and I worked it out how to highlight items when they get clicked and did like they tell here.

But this will highlight the item after it got clicked. I would like to have something like in a normal ListView. So the item should be highlighted while clicking. This is why I used OnTouchListener instead.

@Override
    public boolean onTouch(View v, MotionEvent event) {
        int adapterPosition = getAdapterPosition();
        if (adapterPosition == RecyclerView.NO_POSITION) return false;

        adapter.notifyItemChanged(adapter.getSelectedPos());
        adapter.setSelectedPos(adapterPosition);
        adapter.notifyItemChanged(adapter.getSelectedPos());

        if(event.getAction() == MotionEvent.ACTION_UP){
            clicks.onItemSelected(adapterPosition);
            return true;
        }

        return false;
    }

But my Problem is, that I also want to know when the user moves his finger up, so I can do what I want when it is clicked. But unfortunately this does not work. How can I achieve this?

progNewbie
  • 4,362
  • 9
  • 48
  • 107

3 Answers3

4

The code you provided, I don't think it is right approach to just highlight the selected item. Your code will notify items unnecessary many times.

This can be done with a selector drawable. As there are many answers which guide, how to use a selector background.

But here is the problem

selectableItemBackground just works for the touch, not selection. So here is a hack.

We will set foreground as ?selectableItemBackground and a custom background selector on same view.

I created a sample which fulfill your requirement.

  • First make a background selector which sets transparent color if deactivated, and a color when activated.

activated_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true">
        <color android:color="@color/colorPrimary"/>
    </item>
    <item>
        <color android:color="@android:color/transparent"/>
    </item>
</selector>
  • Use this selector as your item's background. Also set foreground as ?selectableItemBackground.

row.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@drawable/activated_color_selector"
    android:foreground="?selectableItemBackground">

    // other views inside

</LinearLayout>
  • Your model class will hold selected boolean.

Model.java

public class BaseModel extends BaseObservable {
    private boolean selected;
    // setter getter
}
  • Attach model selected field to the View. Also invert selected field when clicked.

Inside RecyclerView adapter

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    final BaseModel model = list.get(position);
    holder.linearLayout.setActivated(model.isSelected());
    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            model.setSelected(!model.isSelected());
            notifyItemChanged(position);
        }
    });
}

All done!

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

For adding a background color to your item while clicking you can add android:background property to your parent item view(which is declared inside your adapter layout).

Add background_selector.xml inside drawable

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

Use above xml like below inside your adapter layout:

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
-1

try adding this attribute to the XML of your recycler view

android:background="?android:attr/selectableItemBackground"

and if this doesn't work take a look here it may help you :

How to properly highlight selected item on RecyclerView?

  <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
    />

let us suppose this is ur item view add this attribute android:background="?android:attr/selectableItemBackground" also to the parent, in this case, it is the relative layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"/>
</RelativeLayout>
progNewbie
  • 4,362
  • 9
  • 48
  • 107
Badran
  • 515
  • 1
  • 7
  • 21
  • Why should setting the background color help me? And the link you posted is exactly the same I already posted in my initial question. – progNewbie Aug 21 '18 at 13:14
  • Oh okay , I am sorry my fault I just woke up :p please try adding the attribute to your recycler view and let me know if it worked – Badran Aug 21 '18 at 13:16
  • No problem. Sadly it does not work. It just makes all my items gray. – progNewbie Aug 21 '18 at 13:20
  • are you using cardView ? – Badran Aug 21 '18 at 13:22
  • and try adding this attributes android:clickable="true" and android:focusable="true" with the attribute I gave you above – Badran Aug 21 '18 at 13:24
  • I added all of them, sadly does not work, still all gray now. Do you mean adding them on the recyclerview or the view of the element thet gets loaded into the recyclerview? I am not using CardView – progNewbie Aug 21 '18 at 13:37
  • on the recycleview xml not on the elements(items) that get loaded,the parent layout not the items of it , if it worked we can change the color as needed but first let us check if that will work – Badran Aug 21 '18 at 13:40
  • oh I just edited my answer , but it seems it's not working ... I will try to find you a solution asap – Badran Aug 21 '18 at 13:48
  • my bad again I am sorry , I just edited the solution again try it now... I am sorry again – Badran Aug 21 '18 at 14:08
  • You don't need to apologize. You are trying to help :) I tried this, but unfortunaltely with no success. – progNewbie Aug 21 '18 at 14:29