在ExpandableListView中添加组项目之间的差距

时间:2012-12-03 07:44:13

标签: android android-layout

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

2 个答案:

答案 0 :(得分:7)

不确定你的目标是什么,但这是一个想法

将主活动中的列表传递到自定义列表

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;
}

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

答案 1 :(得分: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"
        />