設置 android:listSelector="@drawable/list_selector",點擊項目有效果,但按下前無默認背景,或狀態切換異常。
規則:Selector 從上到下匹配,第一個匹配的項目生效。默認項目 (無狀態限制) 必須放在最後!
android:focusable="true"
android:choiceMode="singleChoice"
android:divider="@null"
android:dividerHeight="0dp" />
若項目佈局根視圖設置了 android:background,會覆蓋 Selector。解決:
android:background="?android:attr/selectableItemBackground" 使用主題默認選擇器android:background="@null"
// RecyclerView 無 listSelector,用 ItemDecoration 繪制選中/按下態
recyclerView.addItemDecoration(new ItemDecoration() {
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
// 繪制選中項目背景
}
});
// 或更簡單:項目佈局使用前景屬性 (API 23+)
android:drawable="..." 在最後android:listSelector 引用正確、drawSelectorOnTop="true"selectableItemBackgroundchoiceMode 設置觸發 state_selected