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 TextView
s and Adapter
s 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
Application Content Goes Behind the Navigation Bar in Android L
Android:Load Svg File from Web and Show It on Image View
Folder Added on Android Is Not Visible via Usb
My Android Camera Uri Is Returning a Null Value, But the Samsung Fix Is in Place, Help
Mediaplayer Setdatasource, Better to Use Path or Filedescriptor
Speech to Text from Own Sound File
Activity Killed/Oncreate Called After Taking Picture via Intent
How to Use Mediacodec Without Mediaextractor for H264
Rendering Problems in Android Studio V 1.1/1.2
How to Return String or JSONobject from Asynchronous Callback Using Retrofit
Include Stetho Only in the Debug Build Variant
Android Sax Parser Not Getting Full Text from Between Tags
Practical Way to Find Out If Sms Has Been Sent
Android Sp VS Dp Texts - What Would Adjust the 'Scale' and What Is the Philosophy of Support