Đặ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.
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
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