2010-01-12 12 views
7

tôi có một cái nhìn danh sách và một bộ chuyển đổi mà bộ màu nền xen kẽ cho các mục danh sách ("ngựa vằn" danh sách phong cách):Liệt kê các mục với màu sắc xen kẽ

public View getView(final int position, View convertView, ViewGroup parent) { 
    int colorPos = position % colors.length; 
    ... 
    convertView.setBackgroundColor(colors[colorPos]); 
    return convertView; 
} 

Nhưng bây giờ, khi tôi chọn một mục sử dụng cuộn bánh xe, hoặc khi tôi nhấp vào một mục, màu sắc ban đầu để chọn/nhấp chuột không ghi đè lên nền tùy chỉnh của tôi (tôi có thể thấy màu gốc bên dưới màu tôi đặt).

Làm cách nào để đặt màu gốc cho các trạng thái này?

Trả lời

20

Tôi nghĩ cách dễ nhất là tạo hai bộ chọn được sử dụng làm tài nguyên nền, với màu trong suốt ở chế độ state_selected: (res/drawable/alterselector1.xm l :)

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" 
     android:drawable="@drawable/transparent" /> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/transparent" /> 
    <item android:state_selected="false" 
     android:drawable="@drawable/altercolor1"/> 

</selector> 

(res/drawable/alterselector2.xml :)

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" 
     android:drawable="@drawable/transparent" /> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/transparent" /> 
    <item android:state_selected="false" 
     android:drawable="@drawable/altercolor2"/> 
</selector> 

(res/values ​​/ colors.xml :)

<resources> 
    <drawable name="transparent">#00ffffff</drawable> 
    <drawable name="altercolor1">#ffffffff</drawable> 
    <drawable name="altercolor2">#ff000000</drawable> 
</resources> 

Sau đó, bạn thiết lập các hình nền trong phương thức getView của bộ điều hợp với phương thức setBackgroundResource:

if (position % 2 == 0){ 
    reusableView.setBackgroundResource(R.drawable.alterselector1); 
} else { 
    reusableView.setBackgroundResource(R.drawable.alterselector2); 
} 

Bây giờ khi bạn chọn một hàng, nền của bạn sẽ không ẩn bộ chọn gốc đằng sau.

+0

Điều này hoạt động một phần - Tôi có thể thấy đánh dấu khi mục được lấy nét bằng cách sử dụng nút cuộn, nhưng tôi không thể làm cho nó hoạt động khi mục được nhấn. Tôi đã thử tất cả các trạng thái được liệt kê ở đây: http://developer.android.com/guide/topics/resources/color-list-resource.html, nhưng không có gì hoạt động ... – zorglub76

+1

Tôi đã chỉnh sửa các bộ chọn để xử lý trạng thái được nhấn . Có vẻ như khi bạn nhấn mục, nó sẽ mất trạng thái đã chọn của nó. Vì vậy, bạn phải xác định trạng thái nhấn để được minh bạch quá. Chỉ cần quan tâm đến thứ tự, bởi vì bộ chọn sẽ sử dụng mục đầu tiên khớp với trạng thái hiện tại, vì vậy mục state_selected = "false" sẽ ở dưới cùng. – Utyi

+0

Hoạt động! Gần một năm kể từ khi tôi hỏi câu hỏi này !! Cảm ơn! – zorglub76

2

Bạn cần phải thay đổi danh sách của bạn nổi bật màu nếu bạn làm điều đó thông qua phong cách

<style name="Widget.AbsListView"> 
     <item name="android:listSelector">@drawable/my_selector</item> 
</style> 

hoặc bạn có thể đặt cùng một thuộc tính trong mã my_selector là một trạng thái drawable - tìm kiếm ví dụ trong thư mục SDK:

<?xml version="1.0" encoding="utf-8"?> 
<!-- Copyright (C) 2008 The Android Open Source Project 

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 

      http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
--> 

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

    <item android:state_window_focused="false" 
     android:drawable="@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> 
+0

Tôi đã thử điều đó, nhưng bất cứ điều gì tôi đặt cho nền lúc đầu, bị ghi đè bằng cách tạo "ngựa vằn" trong mã sau ... Có cách nào khác để tạo hình dáng ngựa vằn không? – zorglub76