Android Expandablelistview the perfect solution to the event _android

Source: Internet
Author: User
About Expandablelistview by event processing, many online are using the context menu to register to Expandablelistview on the implementation of long events.

The disadvantage is obvious, not flexible enough, can not be separated from the parent, child, parent, and children between the pop-up content.
The following is my solution, the method is a bit opportunistic. First of all, the use of my method must use a custom baseexpandablelistadapter, as for why, specifically, later mentioned.

the Expandablelistview itself has inherited from Adapterview Setonitemlongclicklistener (Adapterview.onitemlongclicklistener Listener Method
To implement the Listener:
Copy Code code as follows:

/**
* Long by Mailbox shortcut options
* @author King
*/
Private class Quickwaylistener implements onitemlongclicklistener{
@Override
public boolean Onitemlongclick (adapterview<?> arg0, view view,
int POS, long id) {
POS Unavailable description See below
return false;
}
}

If this method is used in ListView by events just fine, but in Expandablelistview, the third parameter POS cannot be clicked separately by the parent or child, and which parent or subkey.

in the Expandablelistview response Onitemlongckick method, the value of the POS parameter is: From top to bottom, parent + show the number of subkeys to the click Position (Note: is displayed, hidden subkeys are not included, starting from 0).
For example:
Parent 1 (Hide 3 subkeys)
Parent Item 2
|-Child Item 2-0
|-Child Item 2-1
|-Child Item 2-2
The POS value is 3 when a long press subkey is 2-1. Obviously, depending on the POS value, it is not possible to determine which subkey or parent is clicked.
Therefore, relying on POS is difficult to handle the click position.

if you can directly in the Onitemlongclick method to obtain Grouppos, and childpos should be much better?

So I saw the Onitemlongclick method The second argument: view. The view here is the view that corresponds to the position you are in. View has a method Gettag (int key). If you create this view by setting the Grouppos,childpos through the Settag (int key, Object value), in response to Onitemlongclick, you can take it out directly.

Now it's time to talk about the reasons why you have to use custom baseexpandablelistadapter.

To bind Grouppos,childpos to view in a settag way, you must manipulate the creation of the view. To control this process, you must override the Getgroupview and Getchildview methods in the custom Baseexpandablelistadapter. As follows:
Copy Code code as follows:

public class Accountlistadapter extends Baseexpandablelistadapter {
... Omit Other methods
@Override
Public View getchildview (int groupposition, int childposition,
Boolean islastchild, View Convertview, ViewGroup parent) {
I only create TextView to display text by my own Mkchildview () method, more complex to populate a view with Layoutinflater
TextView CHILDTV = Mkchildview ();
Mark Position
The resource ID must be used when the key (not a Run-time exception for the resource ID), and Android is meant to use tag to hold the resource ID counterpart.
The groupposition,childposition is saved through the Settag, and can be directly obtained through the view parameter in the Onitemlongclick Method!
Childtv.settag (r.id.xxx01, groupposition);
Childtv.settag (r.id.xxx02, childposition);
return CHILDTV;
}
@Override
Public View getgroupview (int groupposition, Boolean isexpanded,
View Convertview, ViewGroup parent) {
TextView GROUPTV = Mkgroupview ();
Set Same as Getchildview
Grouptv.settag (r.id.xxx01, groupposition);
Grouptv.settag (r.id.xxx02,-1); The setting-1 indicates that the parent is the one who clicks on time, so it's good to judge.
Grouptv.settext (Groups[groupposition]);
return GROUPTV;
}
}

Having completed this step, we only need to expandablelistview the response Onitemlongclick method through View.gettag (r.id.xxx01), View.gettag (r.id.xxx02) Can get grouppos,childpos.
As follows:
Copy Code code as follows:

/**
* Long by Mailbox shortcut options
* @author King
*/
Private class Quickwaylistener implements onitemlongclicklistener{
@Override
public boolean Onitemlongclick (adapterview<?> arg0, view view,
int POS, long id) {
int grouppos = (Integer) view.gettag (R.ID.XXX01); Parameter value is the corresponding resource ID number that is used when Settag
int childpos = (Integer) view.gettag (r.id.xxx02);
if (Childpos = = 1) {//long pressed is the parent
According to the Grouppos to determine what you are long by the parent, do the appropriate processing (frame, etc.)
} else {
According to Grouppos and Childpos, determine which of the parent items you are using, and then handle them accordingly.
}
return false;
}
}

This is finished, seemingly long-winded. rewrite Baseexpandablelistadapter write more concise, not understand the friend can first go to the Internet to check how to customize Baseexpandablelistadapter, and custom Baseadapter is actually the same.
Related Article

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.