Tôi đang cố gắng áp dụng bộ chọn cho một ListView để giúp dễ dàng cho những người không có màn hình cảm ứng điều hướng ứng dụng của tôi. Vấn đề là, bằng cách áp dụng bộ chọn vào ListView, nó chỉ có vẻ áp dụng các màu nền cho toàn bộ danh sách, chứ không phải các mục bên trong nó.Android: Toàn bộ ListView thay đổi màu trên tiêu điểm, không chỉ mục con ListView
Bất kỳ ý tưởng nào? Dưới đây là một số mã:
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="@drawable/listselector"
/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There are no Clients yet."
/>
listselector.xml trong thư mục drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector
android:id="@+id/myselector"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/darkblue" />
<item
android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/green" />
<!-- Focused states -->
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/green" />
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/green" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@color/green" />
</selector>
Chỉ cần để giúp bất cứ ai khác ngoài những người có thể có vấn đề này : Tôi đã sửa nó bằng cách áp dụng quyền chọn cho mục con ItemItem dưới dạng thuộc tính android: background và nó đã hoạt động! –
Bạn nên chấp nhận câu trả lời chính xác bên dưới –