1

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>
Ams1
  • 135
  • 1
  • 14
  • 3
    you can do it with adding a `customAdapter` to your lisView and then you can create a complete customized layout for each of your listView items there you can add **margin** and **divider** for each of your items – SepJaPro2.4 Dec 14 '16 at 14:55

3 Answers3

3

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>
Binil
  • 454
  • 3
  • 11
0

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

SepJaPro2.4
  • 704
  • 9
  • 19
0

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?

Community
  • 1
  • 1
Al Blanc
  • 38
  • 6