progNewbie
progNewbie

Reputation: 4822

Highlight selected item in RecyclerView WHILE clicking

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?

Upvotes: 2

Views: 6623

Answers (3)

Khemraj Sharma
Khemraj Sharma

Reputation: 58964

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!

Upvotes: 4

Jitesh Mohite
Jitesh Mohite

Reputation: 34210

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:

Upvotes: 0

Badran
Badran

Reputation: 535

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>

Upvotes: -1

Related Questions