Customize the color of each separator for each item in a list view

I want to have a list with different delimiters between list items. I used this code to define a white delimiter with a height of 1:

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

However, he sets the separator so that the whole element is white, and I want only some of them to be white and others to be different colors.

How can i do this?

+8
android android-layout android-listview
source share
2 answers

Set the separator to a height of 0 and implement the view in the layout of your item with a height of 1 and change its color based on the list item every time you create a view.

Here is a sample XML layout:

 <?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> 

And here is how you change the color in the adapter:

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

Where int color[] is the list of colors that you want to use.

Read more about ViewHolder here .

+9
source share
  • Separate your list item and the entire list in xml
  • Create a custom adapter for your list items and create a list
  • Depending on the criteria that you want to change, the color of the separator for each new item that you add from the adapter to the list

Example:

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

In your activity

  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); 
+1
source share

All Articles