In ExpandableListView, how to detect group collapse?

In my expandableListView, I created a custom button for expanding / smoothing a group and expanding it, but not when resetting.

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 the group crashed:

  • button press = expand group
  • click elsewhere in the group = do something setOnGroupClickListener

when the group expands:

  • button press = collapse group (ok, but ...)
  • clicking elsewhere in the group = collapse of the group, not a reaction from setOnGroupClickListener

Why does setOnGroupClickListener not load when I click on an extended group? How to solve this?

+6
source share
4 answers

I ran into a somewhat similar problem. I need all groups to always be extended and interactive. 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 am really wondering if there is a normal way to do this.

+1
source

// 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(); }}); 
+1
source

very simple using this method

 @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 } ........... } 
+1
source

if you click on a group, it needs to be called every time, but if your button or something else clickable or focused is exceeded / in group mode, it will only call the onClick method of the object on top of the others. If this is not a solution, provide more code.

@folone is the best way:

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

return true should undo most files by default -> expand / collapse

0
source

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


All Articles