在“可展开列表视图”中添加组项之间的间距



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

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

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

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

例如,这应该增加组之间的间距,而不是子级

对于将来,在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"
        />

最新更新