简体   繁体   中英

How to have a gap between group items but not between group and child on ExpandableListView?

I pretty much have the same problem as How to remove specific space between parent and child in an ExpandableListView .

I have a ExpandableListView and I want to separate my groups without having a gap between the group and first child when it is expanded.

I tried removing the divider but it makes my group items close together with no space between them at all.

I also tried using a bottom margin on group and negative margin on child but the child was separated from the parent group.

Here is my code:

<ExpandableListView android:id="@android:id/list" android:layout_width="match_parent"
    android:layout_height="match_parent" android:groupIndicator="@null"
    android:layout_gravity="center_horizontal|top"
    android:divider="@android:color/transparent"
    android:dividerHeight="8dp"
    android:childDivider="#00000000"
    android:layout_marginTop="10dp"/>

Like that my groups have a nice gap between them but when I expand them the first child is separated from the parent by that same 8dp height.

By far the best solution I found is to put the dividerHeight to "0dp" and add a paddingTop to the groupItem in the xml. Doing that you have not divider between Group and it childs but you will have it between groups. Maybe it isn't the best solution of history but, you know, ExpandableListView is not the best designed view of the history...

Inside code:

where you have the ExpandableListView:

<ExpandableListView
    [...]
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"/>

Where you have the group item xml in the parent view:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp">

In this example I put a "divider" of 10dp between groups but not between a group and it children.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM