在列表视图中为每个元素设置不同的分隔线颜色

我想列表元素之间有不同的分隔符列表。 我已经使用这个代码来定义一个高度为1的白色分隔线:

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

然而,它将所有元素的分隔符设置为白色,我只希望其中一些是白色的,另一种是不同颜色的。

我怎样才能做到这一点?

Related of "在列表视图中为每个元素设置不同的分隔线颜色"

将分隔线的高度设置为0,并在高度为1的项目布局中实现视图,并在每次构build视图时根据列表项更改其颜色。

这是一个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> 

这就是你如何改变适配器的颜色:

 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; } } 

其中int color[]是要使用的颜色的列表。

更多关于ViewHolder的信息请看这里 。

  1. 用xml分隔你的列表项目和你的整个列表
  2. 为列表项目创build自定义适配器并实例化您的列表
  3. 取决于您要更改从适配器添加到列表中的每个新项目的分隔线颜色的标准

例:

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; } } 

在你的活动

  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);