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

时间:2012-10-17 12:36:41

标签: android android-layout android-listview divider

我希望在列表元素之间有一个包含不同分隔符的列表。 我使用此代码来定义高度为1的白色分隔符:

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

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

我该怎么做?

2 个答案:

答案 0 :(得分:9)

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

这是一个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 read here的更多信息。

答案 1 :(得分:2)

  1. 将您的列表项和整个列表分隔为xml
  2. 为列表项创建自定义适配器并实例化您的 列表
  3. 根据您要更改分隔符颜色的条件 您从适配器添加到列表中的每个新项目
  4. 示例:

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