编程知识 cdmana.com

Invalid setting of selector background for Android recyclerview item

Android Recyclerview item Set up selector Background ineffectiveness

One 、 stay selector Of xml The default state in the file is placed in front of the click status , Result in invalidity ,( My understanding is that ) When it satisfies the first state , You don't read other States down .

	<item android:drawable="@color/white"/>
 	<item  android:state_pressed="true"   android:drawable="@color/gainsboro"/>

therefore , To put the default state at the end

	<item  android:state_pressed="true"   android:drawable="@color/gainsboro"/>
    <item android:drawable="@color/white"/>

Two 、 Set under the right conditions selector The background doesn't work either . There are two solutions , You can choose either one of them .
1. Set the background and put clickable Property is set to true

	android:clickable="true"
    android:background="@drawable/selector_list_item"

2. to recycerview Of item Add click event .
notes :recycleview There are no ready-made click events , Need to customize click events . Want to learn about custom click events , Please click on the link below . stay recycleview How to customize click events .

	itemView.setOnClickListener(this);

版权声明
本文为[osc_ iqngllfn]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224102147067n.html

Scroll to Top