Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pressed state for RecyclerView Item

I'm using horizontal recyclerview as a custom bottom navigation bar. I want to define a pressed state for the each item so their color can change.

I tried to do it like this but couldn't manage it

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/menu_pressed" android:state_activated="true" />
    <item android:drawable="@color/menu_pressed" android:state_pressed="true" />
    <item android:drawable="@color/menu_pressed" android:state_checked="true" />
    <item android:drawable="@color/menu_pressed" android:state_focused="true" />
    <item android:drawable="@color/menu" />
</selector>

recycler view:

 <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:layout_gravity="bottom"
        android:background="@drawable/menu_selector"
        android:fadingEdge="vertical|horizontal"
        android:fadingEdgeLength="60dp"
        android:fillViewport="false"
        android:requiresFadingEdge="horizontal|vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true">
like image 802
Ege Kuzubasioglu Avatar asked Feb 19 '18 06:02

Ege Kuzubasioglu


People also ask

What is RecyclerView state?

public class RecyclerView.State. Contains useful information about the current RecyclerView state like target scroll position or view focus. State object can also keep arbitrary data, identified by resource ids. Often times, RecyclerView components will need to pass information between each other.

How to implement drag and Drop in RecyclerView android?

Drag and Drop can be added in a RecyclerView using the ItemTouchHelper utility class. Following are the important methods in the ItemTouchHelper. Callback interface which needs to be implemented: isLongPressDragEnabled - return true here to enable long press on the RecyclerView rows for drag and drop.

What is linear layout manager in Android?

This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A RecyclerView. LayoutManager implementation which provides similar functionality to android. widget. ListView .


1 Answers

With a RecyclerView you cant define and set a single selector for every item like we could with ListView. It has to be done at the viewholder level.

So each item layout has to have a selector set as a background.

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="@drawable/menu_selector">

Then in your adapter onBind you can set a click listener and it should work

holder.frame.setOnClickListener {
  // do something
}
like image 175
Eoin Avatar answered Oct 16 '22 17:10

Eoin