Reputation: 135
I want to create a Listview where each item has a divider and a padding or margin to other element, is that possible?
I only saw solutions where somebody used the divider as "padding or margin" because the color is the same as background. But i need both.
my style for the list:
<!-- res/values/styles.xml -->
<style name="ListView" parent="@android:style/Widget.ListView">
<item name="android:background">@color/light_grey</item>
<item name="android:cacheColorHint">@android:color/transparent</item>
<item name="android:divider">@android:color/transparent</item>
<item name="android:dividerHeight">2dp</item>
<item name="android:listSelector">@drawable/list_item_selector</item>
</style>
Upvotes: 1
Views: 842
Reputation: 454
In layout.xml
add divider as color or drawable
, and dividerHeight
as shown below.
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:divider="#ff0000"
android:dividerHeight="1dp">
to get the padding to each list item, Use a custom adapter,and put padding in listitem.xml
layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingRight="8dp"
android:paddingBottom="16dp">
<!--your list item contents-->
</RelativeLayout>
Upvotes: 3
Reputation: 635
you can create a customAdapter
like below :
public class customAdapter extends BaseAdapter {
...
}
and then implement the methods of BaseAdapter
and in the getView
method you can inflate your custom layout for each of the list view items like below :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get view for row item
View rowView = mInflater.inflate(R.layout.list_item_recipe, parent, false);
return rowView;
}
visit this link has a very good example : http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67
Upvotes: 0
Reputation: 48
You can make an ItemDecoration
and use it with a RecyclerView
which is quite the same as a ListView
but way more powerful.
See this question for more details: How to add dividers and spaces between items in RecyclerView?
Upvotes: 0