Android Adapter go to item re-use scheme!

Source: Internet
Author: User

The question of adapter to repeat is actually a very simple question, many people do not understand the List,grid and Recyclerview reuse item and use the principle of viewholder, so on the heavy road to go very bumpy, Here the small feather with everyone to learn and summarize the knowledge of the item, and then use a very concise solution to effectively solve the problem of re-use of item. 1. Recognize the re-use and Viewholder of the item. The purpose of the reuse of item is to solve the large amount of view space consumed by the list data, and the appearance of Viewholder is a scheme that the system does when it solves the data display. This scenario is mainly for duplicate item but the data is different. Use tag to bind to item and then get used. Guaranteed that each created item must have a viewholder, so every time you refresh the item, you have to bind the data and various states once, or the slide will have a recurring problem. 2. From the system principle to go up and repeat. Above we said item through the Viewholder to solve the data duplication scheme, although a bit cumbersome, but effective and feasible. Someone asked: "If I want to change the properties of an item and don't want to reuse it, the other item property will change, what's the way to use it?" ”。 I have also encountered this problem in the recent project. And it's an item that has two sub-item, and these two sub-item also have to touch the same, but can't interfere at the same time. Need to do property animation to change the style!! (Is it a sore egg?) After all, the company wants to be cool, so we need to design! )。 Since item is reused, the child control of item will be the same object. So little feather breeds an idea, since the control is the same, then I can record a currently selected item or one of its controls, if it is the current item and the corresponding position is the same, then it must be the current item can be changed, when sliding to the re-use of the item, Although it is also the current item but position is not the same, so this time to do the default settings. When the slide back to meet the previous conditions, so it will continue to change. Why can you use such a scheme so confidently? The reason is simple. The system is processed when the item is reused, and the item that is reused cannot appear on the screen at the same time as the last item. So we don't have to change every state like Viewholder, because even if the property of the previous item changes with the current Item property, you're not invisible, so we don't have to worry about the way this is going to work! 3 Source code Analysis. Recyclerview's ADAPter begins bindviewholder@overridepublic void Onbindviewholder (Recviewholder holder, final int position) {if (! Utils.listisempty (mtoppaymenttypes) && mtoppaymenttypes.size () > position) {paymenttype Toppaymenttype = Mtoppaymenttypes.get (position); Initpaybtnresource (Holder.toppayviewitem, Position, toppaymenttype); } if (! Utils.listisempty (mbottompaymenttypes) && mbottompaymenttypes.size () > position) {Paymenttype Bottompaymenttype = Mbottompaymenttypes.get (position); Initpaybtnresource (Holder.bottompayviewitem, Position,bottompaymenttype); }}//Set item data private void Initpaybtnresource (View itemview, int position, Paymenttype paymenttype) {TextView paytypename = (TextView) Itemview.findviewbyid (; ImageView paytypeimage = (ImageView) Itemview.findviewbyid (; View paytypelight = Itemview.findviewbyid (; Paytypesource Paysource = Mpaysources.get (Paymenttype); if (Paysource! = null) {Paytypename.settext (paySource.getpaytypename ()); Viewholder Binding Item Data Binditemdata (View itemview, int position, Paymenttype paymenttype); The following statement resolves a scenario in which item re-uses the Modify property to cause other item problems. if (Mcheckedpaytype = = Paymenttype) {startlightanimator (); manimpaylight = Paytypelight;} else {if (manimpaylight = PA Ytypelight) {endlightanimator ();}} } if (Issinglepay) {itemview.setonclicklistener (null); Itemview.setbackgroundresource (R.drawable.pay_item_uncheck) ; } else {Itemview.setonclicklistener (Mclick); Itemview.settag (Paymenttype); Itemview.setbackgroundresource ( R.drawable.pay_item_can_change); }}4. Summarize the program! This scheme is only used when a single item is reused to change, do not need each item to change properties to maintain balance, very simple and intuitive solution to the problem of reuse, from the code of brevity and efficiency of the system execution, are better, the disadvantage is that more than a reference to the memory, Very good value for money on the reliable. Someone is sure to ask the Endlightanimator () at the back, but the animation has ended but the property has changed. Here little feather had to spit a sentence: "Everyone's project needs are not the same, I here is the floating light effect, when not selected I can directly hide the floating light off on the line, as far as this property in that position is not important." So what if it has to be displayed to change the properties? This little feather in 3 said, the default property, that is, for example, you put a robot, to turn him into a thin robot, then the end of the time when the direct assignment of a original robot. The solution is right, the problem is that the need to solve the problem is not the same as we need to follow their own needs to achieve. Forgive small feather can not post all the code (although the code is written by itself, but as the company's commercial products, part of the confidentiality is still needed).

Android Adapter go to item re-use scheme!

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: 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.