Android ListView Onitemclicklistener Detailed _android

Source: Internet
Author: User
Tags response code

When we use ListView, we usually add a response event Android.widget.AdapterView.OnItemClickListener for ListView. This article is mainly on the onitemclicklistener of the position and ID parameters to do a detailed explanation, I believe some people have made some detours on this.

Let's take a look at the official documents.

Position the position of the view in the adapter.

ID The row ID of the item that is clicked.

The two lines do not explain clearly the difference between position and IDs. In addition, we also have a adapter GetView method.
Public abstract View GetView (int position, View Convertview, ViewGroup parent)
There is also a position here.

Initial contact ListView classmate, General will directly inherit Arrayadapter, then (such as me), take for granted that Onitemclick position and GetView is the same AH. So we getitem (position) to get the corresponding data.

So is this code wrong? If there is a mistake, in what circumstances will it be wrong?

The answer to the first question is that when we add Headerview or Footerview to ListView, this code is not necessarily what we want.
The cause of the problem is that when we add Headerview or Footerview to ListView, ListView does Something in Setadapter, which leads The meaning of position in adapter and Onitemclicklistener has changed.

We can look at the realization of Setadapter in ListView

public void Setadapter (ListAdapter adapter) { 
if (madapter!= null && mdatasetobserver null) { 
!= . Unregisterdatasetobserver (Mdatasetobserver); 
Resetlist (); 
Mrecycler.clear (); 
if (Mheaderviewinfos.size () > 0| | mfooterviewinfos.size () > 0) { 
madapter = new Headerviewlistadapter ( Mheaderviewinfos, Mfooterviewinfos, adapter); 
else { 
Madapter = adapter; 

It can be seen that if this ListView exists Headerview or Footerview, it will be wrapped in a layer of headerviewlistadapter outside our incoming adapter, This is a adapter designed to automatically handle Headerview and Footerview. In ListView, the Headerview,footerview is not distinguished by itself. ListView can be understood as the UI (ViewGroup) that is responsible for managing an array of view arrays, Headerview and Footerview are delegated to headerviewlistadapter for processing. (You can also see why the API documentation mentions that Addfooterview and Addheaderview want to be called before the Setadapter function, and that if it is called later, the Headerviewlistadapter will not be generated. Thus resulting in the display of Headerview and Footerview).

Back to the beginning of the question, what is the difference between position and ID. For this, let's find out how position and IDs came in.

Onitemclicklistener in Android.widget.AdapterView public boolean performitemclick (view view, int position, long ID) is called in the function.

Performitemclick is called in Android.widget.AbsListView.PerformClick.run ()

 private class PerformClick extends windowrunnnable implements Runnable {int Mclickmoti 
Onposition; public void Run () {//The data has changed since we posted this action in the event queue,//bail out before bad thing 
s happen if (mdatachanged) return; 
Final ListAdapter adapter = Madapter; 
Final int motionposition = mclickmotionposition; if (adapter!= null && mitemcount > 0 && motionposition!= INVALID_<STRONG>POSITION</STRONG&G T  && Motionposition < Adapter.getcount () && Samewindow ()) {final View view = Getchildat (motionposition 
-Mfirstposition);  
If There is no view, something bad happened (the "view scrolled off"//screen, etc.) and we should cancel the click 
if (view!= null) {Performitemclick (view, Motionposition, Adapter.getitemid (motionposition)); } 
} 
} 
} 

As you can see, position is actually the location of the view that was clicked in ListView. Note that the ListView is not responsible for handling Headerview and footviewer, so this position should be the one clicked view in the array [all Headerview, user added view, all Footerview] (Please refer to the GetView implementation of Headerviewlistadapter). The ID is from adapter.getitemid (position).
For the Arrayadapter Getitemid function, the implementation is return position. ID and position are the same.

However, for Headerviewlistadapter

public long getitemid (int <strong>position</strong>) { 
int numheaders = Getheaderscount (); 
if (madapter!= null && <strong>position</strong> >= numheaders) { 
int adjposition = <stron G>position</strong>-Numheaders; 
int adaptercount = Madapter.getcount (); 
if (Adjposition < Adaptercount) {return 
madapter.getitemid (adjposition); 
} 
} 
return-1; 

The implementation logic is that if position points to Headerview or Footerview, then return-1, otherwise, the location of the user view array is returned.
Other words

Number of Id=position-headerview (ID < headerviewer number + user view), otherwise =-1

Therefore, the correct implementation of Onitemclicklistener is as follows:

void Onitemclick (adapterviewparent, view view, int <strong>position</strong>, long id) { 
if (id = = 1) { c2/>//click on Headerview or <strong>footerView</strong> return 
; 
int realposition= (int) id; 
T Item=getitem (realposition); 
Response Code 
}

Related reading:

Analysis of invalid listview setonitemclicklistener clicks in Android

The above is a small set to introduce the Android ListView Onitemclicklistener detailed, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.