2

如何在组项目之间添加间隙(比如说 20dp)ExpandableListView?我有RelativeLayout作为父母的自定义组布局。向父级添加边距无济于事。

4

2 回答 2

8

不知道你的目标是什么,但这是一个想法

将您在主要活动中获得的列表传递给您的自定义列表

MyExpandableListAdapter myAdapter = new MyExpandableListAdapter(expandableList);

在您的自定义列表类方法中:

private ExpandableListView exp;

public MyExpandableListAdapter(ExpandableListView exp)
{
    this.exp = exp;
}

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

    if (convertView == null)
    {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_child, null);
    }

    exp.setDividerHeight(0);

    return convertView;

}


public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{

    if (convertView == null)
    {
        LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_row, null);
    }

    exp.setDividerHeight(20);

    return convertView;
}

例如,这应该在组之间而不是子组之间添加间距

于 2012-12-03T08:13:56.940 回答
0

对于未来,在 xml 布局中,您可以添加android:dividerHeight"到您的ExpandableListView,也可以调整分隔线颜色:

       <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@color/red"
        android:dividerHeight="5dp"
        android:indicatorLeft="? 
        android:attr/expandableListPreferredItemIndicatorLeft"
        />
于 2021-01-26T06:09:37.957 回答