When we use Expandablelistview, implementation click on a groupview then expand Chidview, then this time, the size of the adapter before and after the change.
For example: Suppose there are 20 groupview, and each groupview has a childview. When all Groupview are not expanded, the size of adapter is 20, and when we expand a groupview, a childview is displayed, the size of adapter is increased by 1. This must be understood.
When we need to add long presses for each groupview, get the Groupview object that is currently being long pressed, the following is the problem that occurs when you use this method.
[Java]View Plaincopy
- @Override
- Public void Oncreatecontextmenu (ContextMenu menu, View V,
- Contextmenuinfo menuinfo) {
- Super.oncreatecontextmenu (menu, V, menuinfo);
- Menuinflater inflater = Getmenuinflater ();
- Inflater.inflate (R.menu.booklist_context, menu);
- Expandablelistview.expandablelistcontextmenuinfo info = (expandablelistview.expandablelistcontextmenuinfo) Menuinfo;
- //Gets the subscript for the current long press
- final int index = Expandablelistview
- . Getpackedpositiongroup (info.packedposition.);
- //system.out.println ("index----------->" + index);
- //system.out.println ("Adapter size------------->" + explistview.getadapter (). GetCount ());
- Mbookinfo = (bookinfodto) explistview.getadapter (). GetItem (index);
- }
1, when all the Groupview are not expanded, can correctly obtain the subscript of each groupview, through the adapter can be bound to the object in this Groupview.
2, but, when some groupview is expanded, it is not possible to use the method above to get the object bound in Groupview. The reason is given above, because the size of the adapter at this time has been changed. The subscript obtained at this time is a relative subscript of 20 groupview. The object corresponding to the subscript in adapter is inconsistent, because the adapter also contains the expanded Childview.
Use the following methods to resolve the problems that are encountered above.
[Java]View Plaincopy
- @Override
- Public void Oncreatecontextmenu (ContextMenu menu, View V,
- Contextmenuinfo menuinfo) {
- Super.oncreatecontextmenu (menu, V, menuinfo);
- Menuinflater inflater = Getmenuinflater ();
- Inflater.inflate (R.menu.booklist_context, menu);
- Expandablelistview.expandablelistcontextmenuinfo info = (expandablelistview.expandablelistcontextmenuinfo) Menuinfo;
- View vin = (view) Info.targetview;
- Mbookinfo = (bookinfodto) vin.gettag ();
- }
In the adapter, set a tag for each groupview, which is the object we need to make a long push.
This allows us to remove the tag from each groupview in the above method, which is the object we have bound to the Groupview.
This will not depend on the change index.
/***************************************************************************************/
You can save the index of the current item with the tag of the control and then get the tag of the control from the front
http://blog.csdn.net/kavensu/article/details/8307178
Use expandablelistview--when the group option is expanded, how to get the group option for long presses correctly.