I'm using the RecyclerView
like below:
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="320dp"
android:layout_height="match_parent"/>
and my list item:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_medium_high">
<com.basf.suvinil.crie.ui.common.widget.CircleView
android:id="@+id/circle"
android:layout_width="22dp"
android:layout_height="22dp"/>
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="57.5dp"/>
</LinearLayout>
see in detail this part android:background="@drawable/selector_medium_high"
it's a normal selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/background_high" android:state_activated="true"/>
<item android:drawable="@color/background_high" android:state_pressed="true"/>
<item android:drawable="@color/background_high" android:state_checked="true"/>
<item android:drawable="@color/background_high" android:state_focused="true"/>
<item android:drawable="@color/background_medium"/>
</selector>
but when I run this code, i have no changes in background color when I touch the row....
Add click listener for item view in .onBindViewHolder () of your RecyclerView's adapter. get currently selected position and change color by .setBackground () for previously selected and current item Show activity on this post. Most Simpler Way From My Side is to Add a variable in adapterPage as last Clicked Position.
Add the property into your xml (where you declare the RecyclerView): Show activity on this post. Add click listener for item view in .onBindViewHolder () of your RecyclerView's adapter. get currently selected position and change color by .setBackground () for previously selected and current item Show activity on this post.
Using recycler view we can show grids and list of items. This example demonstrates How to set the random background for Recyclerview by creating a beautiful student records app that displays student name with age. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
This example demonstrates how do I properly highlight the selected item on android RecyclerView. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Add the following dependency in the build.gradle (Module: app)
Set clickable
, focusable
, focusableInTouchMode
to true
in all elements of RecyclerView
"list".
Add :
android:background="?android:attr/selectableItemBackground"
in item.xml
If nothing of this works for you, like it didn't for me, use this code:
android:foreground="?android:attr/selectableItemBackground"
The trick is in android:foreground
attribute...
Adding android:background="?android:attr/selectableItemBackground"
to my_list_item.xml
's root layout seems to work for me (assuming you want the default selection colour).
Also make sure the root layout's android:layout_width
is match_parent
rather than wrap_content
to ensure that the whole row is selectable.
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