Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android expandablelistview how to set space between groups items

I have expandablelistview and I want to add padding (or margin) between the groups items, I used margin-botton on the group items, it works but now it is also applied to the group item and its childs, I want to keep a space between groups items, not between a group item and its child, i work like this:

main 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" 
    android:background="#FFFFFF">

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:typeface="serif"
        android:textSize="25dip"
        android:textColor="#025f7c"
        android:text="@string/tv_allAddresses"
        android:layout_gravity="center"
        android:layout_marginTop="20dip"
        android:layout_marginBottom="25dip"/>

    <ExpandableListView
        android:id="@+id/elv_all_addresses_addresses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:padding="10dip">
    </ExpandableListView>

</LinearLayout>

group 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
        android:id="@+id/tv_all_address_group_groupName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="30dip"
        android:textColor="#000000"
        android:textSize="20dip"
        android:typeface="serif" />

</LinearLayout>

child 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="horizontal" >

    <TextView
        android:id="@+id/tv_all_address_child_label"
        android:paddingLeft="50dip"
        android:typeface="serif"
        android:textSize="15dip"
        android:textColor="@color/BlueViolet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_all_address_child_value"
        android:textColor="#000000"
        android:layout_marginLeft="10dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
like image 306
Marco Dinatsoli Avatar asked Feb 10 '13 20:02

Marco Dinatsoli


4 Answers

Because you are using adapter that is extending from BaseExpandableListAdapter so you can set padding programmatically by setting padding to group item when the group is not expanding and then remove the padding when the group is expanding and for each group set padding to last child in it.

setting padding to the last child

public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
if (childPosition == groups.get(groupPosition).getChilds().size() - 1) {
            convertView.setPadding(0, 0, 0, 20);
        } else
            convertView.setPadding(0, 0, 0, 0);
        return convertView;
}

setting padding to group item when it is expanding

public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
    if (isExpanded)
            convertView.setPadding(0, 0, 0, 0);
        else
            convertView.setPadding(0, 0, 0, 20);
        return convertView;
    }

Note

I assume that you are using arraylist for your groups and your childs, you can just replace the groups.get(groupPosition).getChilds().size() - 1 by the size of your group depending in your structure

like image 87
William Kinaan Avatar answered Nov 08 '22 20:11

William Kinaan


I case you are looking for a better option, simply add

expandablelsv.setDividerHeight();

after the group and child indicator it works completely fine in my case, give it a try

like image 20
silverFoxA Avatar answered Nov 08 '22 19:11

silverFoxA


check if child is last and add padding on there

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    if (isLastChild) {
        convertView.setPadding(0, 0, 0, 30);
    }

     return convertView; 
}
like image 27
souttab Avatar answered Nov 08 '22 18:11

souttab


These two attributes helps to give space

android:divider="@android:color/transparent"
android:dividerHeight="8dp"
like image 1
Mohanakrrishna Avatar answered Nov 08 '22 20:11

Mohanakrrishna