Identification of the group that was clicked in expandableListView

I am trying to define a view that was clicked in an expandableListView. When I set OnItemLongClickListener , I get an argument that shows me the position of the clicked view inside the list. However, it also takes into account children's views. I would like it to take into account only the groups, so when the group was pressed, I can determine which one was. Is there any way to do this?

+4
android expandablelistview
Apr 27 2018-11-14T00:
source share
2 answers

No, the long parameter is not a packed value, it is an identifier generated by your adapter ( getCombinedChildId() ). Trying to interpret an identifier, even if you create it in a certain way, would be a bad idea. Id - id.

I believe the correct way is to use the ExpandableListView.getExpandableListPosition(flatPos) method. Your "pos" argument passed to the listener is essentially a flat list item. getExpandableListPosition() method returns a packed position, which can then be decoded into separate groups and child positions using the ExpandableListView static methods.

I myself got into this problem today, so I am describing a solution that has found work for me.

+6
Feb 24 2018-12-12T00:
source share

The long id parameter passed by the onItemLongLongClick method is a packed value. You can get the group position using ExpandableListView.getPackedPositionGroup(id) Child position is obtained using ExpandableListView.getPackedPositionChild(id) . If Child == -1, then a long click was in the group element.

The following is an example listener class demonstrating id unpacking.

 private class expandableListLongClickListener implements AdapterView.OnItemLongClickListener { public boolean onItemLongClick (AdapterView<?> p, View v, int pos, long id) { AlertDialog.Builder builder = new AlertDialog.Builder(ctx); builder.setTitle("Long Click Info"); String msg = "pos="+pos+" id="+id; msg += "\ngroup=" + ExpandableListView.getPackedPositionGroup(id); msg += "\nchild=" + ExpandableListView.getPackedPositionChild(id); builder.setMessage(msg); builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } } ); AlertDialog alert = builder.create(); alert.show(); return true; } } 
+2
Feb 21 2018-12-12T00:
source share



All Articles