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">
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.
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.
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 .
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With