Here is one way to do it.
Create your own ListView ROW layout file with Separator view. (TextView header)
listview_row_with_separator.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView style="?android:attr/listSeparatorTextViewStyle" android:id="@+id/textview_header_separator" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" android:text="November, 2012" android:visibility="gone" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView_row_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="November 12, 2012" android:textAppearance="?android:attr/textAppearanceMedium" > </TextView> </LinearLayout> </LinearLayout>
Importent! Make sure your list is sorted somehow (by date)
extend the ArrayAddapter by holding "Your_List_Item" and implement a populateView (Your_List_Item) that shows the separator if the next list item is not equal to the previous list item.
public class Your_List_Adapter extends ArrayAdapter<Your_List_Item> { private Activity mActivity; private String mCurrentMonth = ""; public Your_Adapter(Activity activity) { super(activity, 0); mActivity = activity; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mActivity.getLayoutInflater().inflate( R.layout.listview_row_with_separator, null); holder = new ViewHolder(convertView); convertView.setTag(holder); } else {
ViewHolder class for an adapter that displays the separator if necessary
class ViewHolder { private TextView textView_row_value; public TextView textview_header_separator; public ViewHolder(final View root) { textview_header_separator = (TextView) root.findViewById(R.id.textview_header_separator); textView_row_value = (TextView) root.findViewById(R.id.textView_row_value); } public void populateView(final Your_List_Item your_list_item,final boolean needSeparator) { if (needSeparator) {
source share