Use ExpandableListView -- when the Group option is expanded, how to correctly obtain the Group option of Long-pressed.

Source: Internet
Author: User

When we use ExpandableListView, click a GroupView to expand the ChidView. At this time, the size of the Adapter changes. For example, suppose there are 20 groupviews, and each GroupView has a ChildView. When all groupviews are not expanded, the size of the Adapter is 20. When we expand a GroupView and show a ChildView, the size of the Adapter increases by 1. This must be understood. When we need to add a long-pressed GroupView to obtain the object of the currently long-pressed GroupView, the following problem occurs when we use the following method. [Java] @ 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; // obtain the subscript final int index = ExpandableL IstView. 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 groupviews are not expanded, the subscript of each GroupView can be correctly obtained and the object bound to this GroupView can be obtained through the Adapter. 2. However, when some groupviews are expanded, you cannot use the above method to obtain the objects bound to GroupView. The reason is shown above, because the size of the Adapter has been changed. The obtained subscript is the relative subscript of 20 groupviews. The object corresponding to the subscript In the Adapter is inconsistent, because the Adapter also contains the expanded ChildView. Use the following methods to solve the problem. [Java] @ 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 = (view1_info.tar getView; mBookInfo = (BookInfoDTO) vin. getTag ();} in the settings object, set a Tag for each GroupView, that is, the object proposed when we need to press for a long time. In this way, we can retrieve the Tag in each GroupView in the above method, that is, the object we bind to GroupView. In this way, you do not need to rely on the changed Index.

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.