[Android] A better way to bind Onclicklistener in the GetView method of adapter

Source: Internet
Author: User
Tags call back

The following content is original, welcome reprint, Reprint please specify

From every day blog:http://www.cnblogs.com/tiantianbyconan/p/4146512.html

The method of binding a point-and-click event to each item in the ListView is common in the following way:

1  PublicView GetView (intPositon, View Convertview, ViewGroup parent) {2     if(NULL==Convertview) {3Convertview = Layoutinflater. from(context). Inflate (R.layout.item,NULL);4     }5 6Button Button =Abviewutil.obtainview (Convertview, r.id.item_btn);7Button.setonclicklistener (NewView.onclicklistener () {8 @Override9          Public voidOnClick (View v) {TenToast.maketext (Context,"Position:"+position, Toast.length_short). Show (); One         } A     }); -  -}

And then run, of course, no problem.

But here is a place to optimize, note the code line 7th, each call to the GetView method will set the button's Onclicklistener, will generate a lot of unnecessary onclicklistener objects.

So, we can think of, when generating Convertview, at the same time set button Onclicklistener,convertview is constantly reused, so that Onclicklistener can be constantly taken, That means this step is between lines 3rd and 4th. In this way, the code evolves to the following:

1  PublicView GetView (intPositon, View Convertview, ViewGroup parent) {2     if(NULL==Convertview) {3Convertview = Layoutinflater.from (context). Inflate (R.layout.item,NULL);4Button Button =Abviewutil.obtainview (Convertview, r.id.item_btn);5Button.setonclicklistener (NewView.onclicklistener () {6 @Override7              Public voidOnClick (View v) {8Toast.maketext (Context, "Position:" +position, Toast.length_short). Show ();9             }Ten         }); One     } A}

This code looks fine, but the problem is in the position of the 8th line of the onclick callback, Because the anonymous inner class is used here (this anonymous inner class implements the View.onclicklistener interface), you need to declare position as final if you need to use the position variable in the onclick. Once the final is declared, the position will be used as a private member variable in this anonymous inner class when it is compiled. In this way, the ListView scrolls down, and the item below needs to be displayed will reuse the object of the Onclicklistener implementation class that does not display the button setting in Convertview,convertview, and the callback onclick, The position used is actually the member variable position of the Onclicklistener implementation class (the position value is initialized only at the time of construction!). )。 Therefore, when the ListView has just loaded the data, it is normal to click on the item on the screen when it is not scrolled, but if the view is reused once it is scrolled, then the value of position is lost. So the data obtained by position in the onclick is of course garbled.

Therefore, to solve this problem, we need to get the OnClick method callback when the position is the correct value, we can choose to use the current display of the convertview corresponding position value stored in the Convertview, The Convertview object is then passed into the Onclicklistener and saved, so the code evolves as follows:

1  PublicView GetView (intPositon, View Convertview, ViewGroup parent) {2     if(NULL==Convertview) {3Convertview = Layoutinflater.from (context). Inflate (R.layout.item,NULL);4Button Button =Abviewutil.obtainview (Convertview, r.id.item_btn);5Button.setonclicklistener (NewOnconvertviewclicklistener (Convertview, r.id.ab__id_adapter_item_position) {6 @Override7                  Public voidOnclickcallback (View Registedview,int... positionids) {8Toast.maketext (Context, "position:" + positionids[0], Toast.length_short). Show ();9                 }Ten         }); One     } A Convertview.settag (r.id.ab__id_adapter_item_position, position); -}

Just like the 5th line above, the button binds to Onconvertviewclicklistener, which is an implementation class of Onclicklistener that can be saved convertview to listener, When you call back the OnClick method, you can get the position of the currently displayed item from the saved Convertview (the position is stored in Convertview in the same way as the tag).

Of course, there is a 12th line, which is intended to save the currently displayed position to Convertview, which is provided to Onconvertviewclicklistener to get the current position.

This is OK, you can handle the click event in the Onclickcallback () method, and each button will always have only one onclicklistener.

Note: Using Onconvertviewclicklistener can rely on Androidbucket (https://github.com/wangjiegulu/AndroidBucket) projects

[Android] is a better way to bind Onclicklistener in the GetView method of adapter

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.