2012-10-17 20 views
8

Tôi muốn có danh sách có các ngăn chia khác nhau giữa các phần tử danh sách. Tôi đã sử dụng mã này để xác định một chia trắng với chiều cao 1:Đặt màu dải chia khác nhau cho từng phần tử trong chế độ xem danh sách

_listView.setDivider(new ColorDrawable(Color.WHITE)); 
_listView.setDividerHeight(1); 

Tuy nhiên nó đặt chia cho tất cả các yếu tố để có màu trắng, và tôi chỉ muốn có một số người trong số họ có màu trắng và một ở khác nhau màu.

Tôi có thể làm điều đó bằng cách nào?

+1

Bạn đang cố gắng để màu sắc thay thế, hoặc xác định ngăn màu khác nhau tại khác nhau điểm? – nickhar

+0

Tôi đang cố gắng xác định các màu chia khác nhau cho các thành phần khác nhau phụ thuộc vào nội dung. Tôi đang cố gắng để làm điều đó một cách lập trình khi tôi đang tạo danh sách, nhưng khi tôi xác định màu sắc khác nhau nó thay đổi tất cả các ngăn đến màu này và không chỉ là chia yếu tố cụ thể của tôi muốn thay đổi. – MrBug

Trả lời

9

Đặt dải phân cách thành độ cao bằng 0 và triển khai Chế độ xem trong bố cục mặt hàng của bạn với chiều cao là 1 và thay đổi màu của nó dựa trên mục danh sách mỗi khi chế độ xem được tạo.

Dưới đây là một mẫu layout XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/text" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

    <View 
     android:id="@+id/divider" 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:background="#FFFFFFFF" /> 

</LinearLayout> 

Và đây là cách bạn thay đổi màu sắc trong các bộ chuyển đổi:

public class MyAdapter extends BaseAdapter { 
    /** List of items to show */ 
    private ArrayList<String> items = new ArrayList<String>(); 
    private Context context; 
    private int color[]; 

    public OffersAdapter(Context c, ArrayList<String> items, int color[]) 
    { 
     super(); 
     this.context = c; 
     this.items = items; 
     this.color = color; 
    } 

    public int getCount() { 
     return items.size(); 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder viewHolder; 

    if(null == convertView) 
    { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     convertView = inflater.inflate(R.layout.list_item, parent, false); 

     viewHolder.text = (TextView) convertView.findViewById(R.id.text); 
     viewHolder.divider = (View) convertView.findViewById(R.id.divider); 

     convertView.setTag(viewHolder); 
    } else { 
     //Retrieve the current view 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 

    //This is where you chance the divider color based on the item 
    viewHolder.divider.setBackgroundColor(color[position]); 

    viewHolder.text.setText(items.get(position)); 

    return convertView; 
} 

//Holds the current view 
private static class ViewHolder { 
     public TextView text; 
     public View divider; 
    } 
} 

đâu int color[] là một danh sách các màu sắc bạn muốn sử dụng.

Tìm hiểu thêm về ViewHolder read here.

+0

tôi đã thử điều đó. nhưng sau đó khi nhấn vào phần tử và giữ bạn sẽ thấy màu nền không ở đúng vị trí, nó sẽ bao phủ chế độ xem tôi đã tạo. – MrBug

+0

Đăng mã của bạn. Bạn đã thiết lập một chiều cao? – slybloty

+0

vâng tôi đã làm ... _listView.setDivider (new ColorDrawable (Color.WHITE)); _listView.setDividerHeight (1); – MrBug

1
  1. riêng biệt mục danh sách của bạn và toàn bộ danh sách của bạn trong xml
  2. Tạo một adapter tùy chỉnh cho các hạng mục danh sách của bạn và nhanh chóng danh sách
  3. của bạn Tùy thuộc vào tiêu chí mà bạn muốn thay đổi màu sắc chia cho từng hạng mục mới bạn thêm từ adapter vào danh sách

Ví dụ:

listitem.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/sometext" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

    <View 
     android:id="@+id/customdivider" 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
      /> 

</LinearLayout> 

list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 

    <ListView 
     android:id="@+id/totallynewlist" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 

CustomAdapter.java

public class CustomAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater = null; 

    public SubjectListAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data = d; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 



     public View getView(int position, View convertView, ViewGroup parent) { 
      View vi = convertView; 
      if (convertView == null) 
       vi = inflater.inflate(R.layout.listitem, null); 

     //Find the divider here and depending on your criteria change the colour - if required take data from main activity if you need it to change the colour 
     View divider= (View) vi.findViewById(R.id.customdivider); 
     if(your criteria) 
      divider.setBackgroundColor(R.color.white); //assuming white is defined in    colors 
     else 
     set to whatever color 
     return vi; 
     } 
} 

Trong Hoạt động của bạn

ListView list = (ListView) findViewById(R.id.totallynewlist); 

    // creating new HashMap 
    HashMap<String, String> map = new HashMap<String, String>(); 
    <//Pass whatever condition you want for your criteria here if you need to - optional> 
    ListPass.add(map); 

    adapter = new CustomAdapter(this, ListPass); 

    list.setAdapter(adapter); 
+0

nhưng phương pháp nào thay đổi bộ chia chỉ cho một phần tử cụ thể? phương thức setDivider thay đổi màu chia cho tất cả các phần tử. – MrBug

+0

Điều này về cơ bản không sử dụng bộ chia - nó tạo ra một cái nhìn của 1 dp, mà bạn có thể thay đổi màu sắc cho mỗi mục danh sách tùy thuộc vào tiêu chí của bạn. Thay đổi nền của phân chia xem mục thành bất kỳ màu nào bạn cần – Slartibartfast

+0

Chúng tôi hiện đang thay đổi nó cho mỗi hàng của danh sách, vì vậy không sử dụng công cụ chia danh sách - đó là phương pháp danh sách và áp dụng cho toàn bộ danh sách – Slartibartfast