2011-12-13 34 views
5

Tôi đã đặt công cụ chọn danh sách tùy chỉnh cho Đơn giản ListView nhưng khi tôi chọn một mục, toàn bộ ListView sẽ chuyển sang màu xanh lam. Tôi không hiểu vấn đề ở đâu.Làm nổi bật hành vi bí ẩn trong ListView

Đây là ListView của tôi:

<ListView android:id="@+id/TopListView" android:layout_width="fill_parent" 
    android:listSelector="@drawable/regular_selector" 
    android:layout_height="wrap_content"> 
</ListView> 

và đó là regular_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="false" android:state_focused="true" 
     android:drawable="@color/transparent" /> 
    <item android:state_pressed="true" 
     android:drawable="@color/blue" /> 
    <item android:state_focused="true" 
     android:drawable="@color/blue" /> 
</selector> 
+0

vì vậy bạn đã nhận được soln? nếu không bạn có thể cho biết kết quả mong đợi của bạn là gì? – havexz

Trả lời

0

Tôi đoán nó là vấn đề bình thường. Bạn phải đặt điều này trong chế độ xem danh sách của mình:

android:cacheColorHint="#00000000" 

Mục tiêu là tắt tối ưu hóa khuôn khổ để cải thiện hiệu suất vẽ trong khi di chuyển.

+0

Đó là kết quả tương tự ... – Maxime

0

Khi tôi đặt regular_selector.xml làm nền mục danh sách xem, nó hoạt động!

8

Tôi đã gặp vấn đề tương tự với chế độ xem toàn bộ danh sách, khi sử dụng màu làm nền. Tò mò này chỉ xảy ra bên dưới api 11.

Giải pháp là sử dụng một hình dạng drawable rắn để bọc các màu:

list_selector_shaped_background_press.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <solid android:color="@color/list_selector_pressed"/> 
</shape> 

List_selector_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 

     <item android:state_window_focused="false" 
     android:drawable="@android:color/transparent" /> 

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. --> 
    <item android:state_focused="true" android:state_enabled="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_disabled" /> 
    <item android:state_focused="true" android:state_enabled="false" 
     android:drawable="@drawable/list_selector_background_disabled" /> 

    <!-- this has to be in a shaped drawable otherwise the whole list is highlighted selects --> 
    <item android:state_focused="true" android:state_pressed="true" 
     android:drawable="@drawable/list_selector_shaped_background_pressed" /> 

     <item android:state_focused="false" android:state_pressed="true" 
     android:drawable="@drawable/list_selector_shaped_background_pressed" /> 

    <item android:state_focused="true" 
     android:drawable="@drawable/list_selector_background_focus" /> 

</selector> 
+0

Cảm ơn rất nhiều ... Tôi đã tìm kiếm giải pháp từ một vài tuần nay. Điều này thực sự đã giúp! :) – Scrat

Các vấn đề liên quan