In an ExpandableListView, how to detect the group collapsing?
Asked Answered
R

4

6

In my expandableListView I've made a custom button to expand/collapse the group and for expanding it works, but when collapsing no.

with this code

listView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Log.d("group click");
                return true;
            }
        });

        listView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Log.d("group collapse");

            }
        });

        listView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Log.d("group expand");
            }
        });

With this code: when group is collapsed:

  • clicking on button = expand the group
  • clicking anywhere else on the group = do something handle by setOnGroupClickListener

when group is expanded:

  • clicking on button = collapse the group (ok but...)
  • clicking anywhere else on the group = collapse the group and not reaction from setOnGroupClickListener

Why setOnGroupClickListener is not loaded when I click on an expanded group ? How to solve that ?

Raposa answered 12/4, 2011 at 20:39 Comment(0)
D
1

I've faced a somewhat similar problem. I needed all groups to always be expanded and clickable. To make it work, I wrote this monkey-code in my ExpandableListActivity:

public void onCreate(Bundle savedInstanceState) {
    ...
    // Expanding all.
    for(int i = 0; i < adapter.getGroupCount(); i++)
        getExpandableListView().expandGroup(i);
    ...
}

@Override
public boolean onGroupClick(ExpandableListView parent, View v,
        int groupPosition, long id) {
    overrideStupidListBehaviour(groupPosition);
    return false;
}

@Override
public void onGroupCollapse(int groupPosition) {
    // Forbidding it to collapse.
    getExpandableListView().expandGroup(groupPosition);
    overrideStupidListBehaviour(groupPosition);
}

private void overrideStupidListBehaviour(int groupPosition) {
    // Code to do when onGroupClick is called
}

I'm really interested, if there is a normal way to do that.

Decarburize answered 19/4, 2011 at 13:12 Comment(0)
M
1

// Listview Group collasped listener

expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() { 
@Override
public void onGroupCollapse(int groupPosition) {
    Toast.makeText(getApplicationContext(),
            listDataHeader.get(groupPosition) + " Collapsed",
            Toast.LENGTH_SHORT).show();

}});
Mussolini answered 23/5, 2014 at 9:11 Comment(0)
T
1

its very simple using this way

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {

if(isExpanded){
//do something when expanded

}else{
//do something when collapsed or not expanded

}
...........
}
Tia answered 16/6, 2017 at 6:45 Comment(0)
L
0

if you click on a group it should be called every time, but if your button or something other clickable or focusable is over/in the group view it will only trigger the onClick method of the object on top of the others... if this is not the solution please provide more code.

@folone there is a better way:

@Override
public boolean onGroupClick(ExpandableListView parent, View v,
        int groupPosition, long id) {
    doSomething();
    return true;
}

return true should cancel most default stuff -> expand/collapse

Luck answered 4/10, 2013 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.