Android: Custom Separator (Or Even Item) in Listview Depening on Content of Item

Android: custom separator (or even item) in ListView depening on content of item

Here is one implementation that does exactly what you describe.

That one is GPLv3, because it is derived from this implementation, which was GPLv3.

You can also use my MergeAdapter for this, which has the advantage of being Apache License 2.0. Just hand it an alternating set of header TextViews and Adapters containing each section's worth of content.

Or, you can peek at all of these and roll their behaviors into your existing Adapter class. The trick is to return your TextView headers at the right spot, and properly implement methods like getViewTypeCount(), getItemViewType(), areAllItemsEnabled(), and isEnabled().

Separator (divider) after last item of ListView

The answer is very simple: you should change android:layout_height="wrap_content" to android:layout_height="match_parent" in your ListView.

You can probably guess why this happens.

How to make divider part of each item in listview in android?

A way to do this would be to include the divider at the bottom of each row. Now you have to set the height of the divider view in your getView method in your listadapter depending of the item you are showing at the moment.

ListView dividers on top and bottom of the ListView Item

There are several ways to achieve this. One simple way would be to hide dividers completely (set divider width to 0 or divider to null) in the list and just have each list item to contain a line at the top and a line at the bottom. Consider this xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp">

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:background="@android:color/white" />

<LinearLayout ...>
<!-- this is your current list item LinearLayout -->
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@android:color/black" />

</RelativeLayout>

As you are already using a custom adapter, it's just a matter of using an XML like above for your list item layout in the getView method of the Adapter. This will produce list items 60dp high with a white line along the top and a black line along the bottom.

Flutter : How can I add divider between each List Item in my code?

There are a number of ways to do the same thing. Let me compare them here.

For a short static list

Use ListTile.divideTiles


ListView(
children: ListTile.divideTiles( // <-- ListTile.divideTiles
context: context,
tiles: [
ListTile(
title: Text('Horse'),
),
ListTile(
title: Text('Cow'),
),
ListTile(
title: Text('Camel'),
),
ListTile(
title: Text('Sheep'),
),
ListTile(
title: Text('Goat'),
),
]
).toList(),
)

For a long dynamic list

Use ListView.separated.


ListView.separated(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: Text('$index sheep'),
);
},
separatorBuilder: (context, index) {
return Divider();
},
)

This returns two widgets for every item, except for the last item. The separatorBuilder is used to add the divider.

For adding a divider after the last item

Create a custom item widget that uses a Divider or BoxDecoration.

Using Divider


final items = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];

@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return Column(
children: <Widget>[
ListTile(
title: Text(items[index]),
),
Divider(), // <-- Divider
],
);
},
);
}

Using BoxDecoration


final items = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];

@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return Container(
decoration: BoxDecoration( // <-- BoxDecoration
border: Border(bottom: BorderSide()),
),
child: ListTile(
title: Text(items[index]),
),
);
},
);
}

Both Divider and BoxDecoration are customizable as far as the line height and color go. Divider also has an indent option, but you could get a BoxDecoration to do the same thing with some padding.

For more style

Use a Card


final items = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];

@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return Card( // <-- Card
child: ListTile(
title: Text(items[index]),
),
);
},
);
}

Show divider after the last item in the list

Try following in XML

<View
android:background="#00ff00"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/YOUR_LIST_ID" />


Related Topics



Leave a reply



Submit