Code example for ExpandableListView

Methods: getChildAtgetExpandableListAdaptergetExpandableListPositiongetFlatListPositionisGroupExpanded, getAdapter, getFirstVisiblePosition, getLastVisiblePosition

0
    public View getHeaderView() {
        return headerView;
    } 
 
    public void configureHeaderView() { 
        if (listView.getAdapter()==null) {
            return; 
        } 
 
        int firstVisiblePosition = listView.getFirstVisiblePosition();
        long expandableListPosition = listView.getExpandableListPosition(firstVisiblePosition);
 
        final int group = getPackedPositionGroup(expandableListPosition);
 
        if (firstVisiblePosition < 0 || !listView.isGroupExpanded(group) ||
                groupHeaderExactlyAtTopOfListView(firstVisiblePosition, group)) {
            if (headerView !=null) {
                headerView.setVisibility(GONE);
            } 
            return; 
        } 
 
        headerView = listView.getExpandableListAdapter().getGroupView(group, false, headerView, parentViewGroup);
        headerViewGroupAttacher.attach(headerView);
        headerView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                listView.collapseGroup(group);
                listView.setSelectedGroup(group);
            } 
        }); 
 
        int headerTop = calculatePushOffset(group, firstVisiblePosition);
        Drawable background = new ColorDrawable(Color.WHITE); // header.getBackground()
        headerView.setBackgroundDrawable(background);
        int measuredHeight = headerView.getMeasuredHeight();
        if (headerView.getTop() != headerTop || headerView.getHeight()!= measuredHeight) {
            headerView.layout(0, headerTop,
                    headerView.getMeasuredWidth(), measuredHeight + headerTop);
        } 
        headerView.setVisibility(VISIBLE);
    } 
 
    private boolean groupHeaderExactlyAtTopOfListView(int firstVisiblePosition, int group) {
        return (firstVisiblePosition==listView.getFlatListPosition(getPackedPositionForGroup(group)) && listView.getChildAt(0).getTop()>=0);
    } 
 
    private int calculatePushOffset(int group, int firstVisiblePosition) {
        int nextHeaderGroup = group + 1;
        int headerHeight = headerView.getHeight();
        int headerHeightWithBuffer = headerHeight + HEADER_TO_HEADER_BUFFER;
 
        int headerTop=0;
        int nextHeaderFlatListPosition = listView.getFlatListPosition(getPackedPositionForGroup(nextHeaderGroup));
        if (nextHeaderFlatListPosition<=listView.getLastVisiblePosition()) {
            // next header is visible, we may need to handle partial push-up 
            View v = listView.getChildAt(nextHeaderFlatListPosition-firstVisiblePosition);
            if (v!=null && v.getTop() < headerHeightWithBuffer) {
                headerTop = round((v.getTop() - headerHeightWithBuffer)*1.1f); // make header 'zoom' away from next header
            } 
        } 
        return headerTop;