RecycleView中選中某一個item變色

1.通過drawable的selector屬性

chk_payitem_select_status.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_recharge_item_bg" android:state_focused="false" />
    <item android:drawable="@drawable/shape_recharge_itemselected_bg" android:state_focused="true" />

</selector>

2.使該item可以獲得焦點,和點擊效果

android:clickable="true"  
android:focusableInTouchMode="true"  
android:focusable="true"

例如:

<RelativeLayout
        android:id="@+id/rlItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/chk_payitem_select_status"
        android:clickable="true"
        android:focusableInTouchMode="true"
        android:focusable="true">
</RelativeLayout>

這樣,當點擊某個條目,該條目背景顏色相應改變

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章