android expandablelistview how to set space between groups items

后端 未结 6 1468
别那么骄傲
别那么骄傲 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:13

    I solved it by adding two views one to header and other to child item like shown below.

    Header.xml

    
    
    
    
    
        
    
    
    
    
    

    child.xml

    
    
    
    
    
        
    
        
    
    
    
    
    

    Now add this code to your Adapter class. The idea is to show/hide the View when group is expanded/collapsed, show on child last item.

    @Override
    public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        String listCount = "(" + getChildrenCount(listPosition) + ")";
    
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_header, null);
        }
        View view_hidden = convertView.findViewById(R.id.view_hidden);
    
        if (isExpanded) {
            view_hidden.setVisibility(View.GONE);
        } else {
            view_hidden.setVisibility(View.VISIBLE);
        }
    
        return convertView;
    }
    
    @Override
    public View getChildView(int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final Medication medication = (Medication) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.expandable_item, null);
        }
    
        View view_hidden = convertView.findViewById(R.id.view_hidden);
        if (isLastChild) {
            view_hidden.setVisibility(View.VISIBLE);
        } else {
            view_hidden.setVisibility(View.GONE);
        }
    
        return convertView;
    }
    

提交回复
热议问题