2013-06-26 41 views
9

Tôi muốn thả bóng ở mục ListView và cũng áp dụng công cụ chọn tùy chỉnh. Nhưng tôi không biết làm thế nào để áp dụng chúng cùng một lúc ..ListView Mục bóng + bộ chọn tùy chỉnh

Đây là shadow.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item > 
     <shape android:shape="rectangle"> 
       <solid android:color="@android:color/darker_gray" /> 
     </shape> 
    </item> 
    <item 
     android:right="1dp" 
     android:bottom="2dp">   
     <shape android:shape="rectangle"> 
       <solid android:color="@android:color/white"/> 
     </shape> 
    </item>   
</layer-list> 

Và chọn tùy chỉnh:

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

tôi đã cố gắng áp dụng selector để toàn bộ ListView android:listSelector="@drawable/selector.xml" và đổ bóng đến mục ListView android:background="@drawable/shadow.xml" Nhưng trong trường hợp này, mục ListView sẽ có bóng nhưng sẽ không phản ứng khi chạm vào.

Cảm ơn bạn trước

+0

công việc này .. cảm ơn bạn^_ ^ –

Trả lời

8

Tôi đã tìm thấy giải pháp. Lý do tại sao seletor không xuất hiện là cấu trúc Android ListView. Nếu bạn thiết lập background cho List ItemView nó chồng lên Selector, vì vậy bạn không thể nhìn thấy nó. Giải pháp là làm cho nền ItemView trong suốt trên Click.

Đây là listview_item_shadow.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
       <solid android:color="@android:color/darker_gray" /> 
     </shape> 
    </item> 
    <item 
     android:right="1dp" 
     android:bottom="2dp">   
     <shape android:shape="rectangle"> 
       <solid android:color="@android:color/white"/> 
     </shape> 
    </item>   
</layer-list> 

Bây giờ bạn nên sử dụng nó trong selector cho ItemView! - listview_item_backgroundstate.xml Bạn cần phải thiết lập listview_item_backgroundstate.xml làm nền cho mục ListView bạn

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@android:color/transparent"></item> 
    <item android:state_selected="true" android:drawable="@android:color/transparent"></item> 
    <item android:state_focused="true" android:drawable="@android:color/transparent"></item> 
    <item android:drawable="@drawable/listview_item_shadow"></item> 
</selector> 

Và cuối cùng bạn phải thiết lập custom_selector.xml như trong ListView. android:listSelector="@drawable/custom_selector.xml"

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

Để biết thêm thông đọc tuyệt vời tutorial

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