Use expandablelistview--when the group option is expanded, how to get the group option for long presses correctly.

Source: Internet
Author: User

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
  1. @Override
  2. Public void Oncreatecontextmenu (ContextMenu menu, View V,
  3. Contextmenuinfo menuinfo) {
  4. Super.oncreatecontextmenu (menu, V, menuinfo);
  5. Menuinflater inflater = Getmenuinflater ();
  6. Inflater.inflate (R.menu.booklist_context, menu);
  7. Expandablelistview.expandablelistcontextmenuinfo info = (expandablelistview.expandablelistcontextmenuinfo) Menuinfo;
  8. //Gets the subscript for the current long press
  9. final int index = Expandablelistview
  10. . Getpackedpositiongroup (info.packedposition.);
  11. //system.out.println ("index----------->" + index);
  12. //system.out.println ("Adapter size------------->" + explistview.getadapter (). GetCount ());
  13. Mbookinfo = (bookinfodto) explistview.getadapter (). GetItem (index);
  14. }

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
  1. @Override
  2. Public void Oncreatecontextmenu (ContextMenu menu, View V,
  3. Contextmenuinfo menuinfo) {
  4. Super.oncreatecontextmenu (menu, V, menuinfo);
  5. Menuinflater inflater = Getmenuinflater ();
  6. Inflater.inflate (R.menu.booklist_context, menu);
  7. Expandablelistview.expandablelistcontextmenuinfo info = (expandablelistview.expandablelistcontextmenuinfo) Menuinfo;
  8. View vin = (view) Info.targetview;
  9. Mbookinfo = (bookinfodto) vin.gettag ();
  10. }


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.