2011-06-28 33 views
10

Với công cụ chọn mặc định, nhấn và giữ mục danh sách làm nền chuyển tiếp giữa hai màu.Android: Cách đạt được hiệu ứng phát sáng khi nhấn và giữ mục danh sách?

Thay thế bộ chọn bằng công cụ chọn bên dưới sẽ xóa hiệu ứng. Theo số this question, tôi cần một hình động để tái tạo nó. Làm thế nào tôi sẽ làm điều đó trong xml?

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

Trả lời

9

Đây là mã từ list_selector_background:

<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" /> 
     <item android:state_focused="true" android:state_pressed="true" 
       android:drawable="@drawable/list_selector_background_transition" /> 
     <item android:state_focused="false" android:state_pressed="true" 
       android:drawable="@drawable/list_selector_background_transition" /> 
     <item android:state_focused="true" 
       android:drawable="@+drawable/list_selector_background_focus" /> 
</selector> 

Tìm thấy on the web.

Và nó sử dụng chuyển đổi này cho các nhấp chuột bấm dài:

<transition xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/list_selector_background_pressed" /> 
    <item android:drawable="@drawable/list_selector_background_longpress" /> 
</transition> 

Tìm thấy on the web too.

Không có hoạt ảnh cho điều đó. Và hãy nhớ giữ cho các trạng thái của bạn theo cùng một thứ tự, hoặc ít nhất hãy nghĩ về nó nếu bạn hoán đổi chúng, thứ tự là quan trọng.

Cá nhân, tôi thích khi mọi thứ hoạt động theo cách tiêu chuẩn, vì vậy tôi sẽ chỉ cho phép bộ chọn danh sách chuẩn.

Trân trọng, Stéphane

+0

Cảm ơn bạn, điều đó thật tuyệt vời. –

+2

không hoạt động cho bất kỳ ý tưởng nào khác tại sao? Tôi áp dụng quá trình chuyển đổi như bạn đã nói, bạn đã có nghĩa là đối với tệp "list_selector_background_transition.xml", chúng tôi thêm quá trình chuyển đổi mà bạn đã đưa vào khối mã thứ hai? Tôi đã làm điều đó một cách chính xác nhưng chỉ có một màu được dàn dựng mà chỉ "list_selector_background_pressed" được áp dụng – coderVishal

+0

@coderVishal Bạn có sử dụng 'RecyclerView' không? ** Giải pháp này chỉ hoạt động nếu bạn sử dụng một lớp con của 'AbsListView' ** (ví dụ, ListView). Nó sẽ kiểm tra nếu background item có 'TransitionDrawable' và bắt đầu animation. Đó là tiếc là không phải là trường hợp với 'RecyclerView', bạn sẽ phải phát minh ra một số cơ chế để bắt đầu các hình ảnh động mình. – Malcolm

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