Mixing extensible and regular listViews in android

I am trying to create a list similar to the list in call log activity (in Froyo). Here, repeated calls from the same person are grouped into an expandable list item, while others are regular items in the list. How to create such a list? Can this be done with the class ExpandableListView?

+3
source share
1 answer

This should be pretty easy using ExpandableListView. Extend BaseExpandableListAdapter to gain control over how group items and extended list items are displayed, and how events are triggered on them. With this, you can organize this view to display a durable group for one item and an expandable list for multiple items. With such an adapter, you can feed an ExpendableListView to make it work

An example of its implementation below. Please note that here are not all methods that should be implemented as not related to the problem. An element is some imaginary class that can themselves show how they should be displayed.

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private final List<Element> elements;

    public MultiSelectExpandableListAdapter(Context c, List<Element> elements) {
        this.elements = elements;
    }

    public View getGroupView(final int group, final boolean expanded, final View convertView,
                             final ViewGroup parent) {
        if (elements.get(group).shouldBeDisplayedAsAGroup()) {
           // inflate and setup view that displays expandable view header
        } else {
           // inflate and setup view of element that should be displayed as single element
        }

    }

    public View getChildView(final int group, final int child, final boolean lastChild, final View convertView,
                             final ViewGroup parent) {

           // inflate and setup child view

    }
}
+5
source

Source: https://habr.com/ru/post/1784052/


All Articles