android expandablelistview how to set space between groups items

后端 未结 6 1465
别那么骄傲
别那么骄傲 2020-12-30 12:24

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 a

6条回答
  •  情话喂你
    2020-12-30 13:11

    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

提交回复
热议问题