Android Drip---viewholder universal, elegant writing

Source: Internet
Author: User
Tags phoneview


Recently in the project, but also write Viewholder.

Suddenly think of the internet to see there is no good wording!


I don't know if you're too tired of writing viewholder that don't have the technical content.

Look at this. Maybe there will be a harvest!


Then I found the following two articles (Thanks for sharing!).

):


Viewholder Super Simple wording click to jump


Elegant use Viewholder click to jump


And then you sort it out and save it. Province of forgetting. Share it by the way!


Directly post code:


The first type:
Import Android.util.sparsearray;import android.view.view;/** * Viewholder General wording *  from: http://www.eoeandroid.com/ thread-321547-1-1.html *  * @author Liujinpeng * */public class Viewholder {private Viewholder () {} @SuppressWarnings ( "Unchecked") public static <t extends View>t Getchildview (View convertview,int ID) {sparsearray<view> Viewholder = (sparsearray<view>) convertview.gettag (); if (Viewholder = = null) {Viewholder = new sparsearray< View> (); Convertview.settag (Viewholder);} View Childview = viewholder.get (ID), if (Childview = = null) {Childview = Convertview.findviewbyid (ID); Viewholder.put (ID, Childview);} Return (T) Childview;}}

Usage:

@Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {    if (Convertview = = null) {        conver TView = Layoutinflater.from (context)          . Inflate (R.layout.banana_phone, parent, false);    }    ImageView Bananaview = Viewholder.get (Convertview, R.id.banana);    TextView Phoneview = Viewholder.get (Convertview, r.id.phone);    Bananaphone Bananaphone = GetItem (position);    Phoneview.settext (Bananaphone.getphone ());    Bananaview.setimageresource (Bananaphone.getbanana ());    return Convertview;}

Another type of:
Import Android.util.sparsearray;import android.view.view;/** * Viewholder Elegant notation * from: http://stackvoid.com/ using-adapter-in-efficiency-way/* * @author Liujinpeng * */public class ViewHolder2 {private Final SPARSEARRAY&LT;VIEW&G T Views;private View convertview;private ViewHolder2 (context context, int layoutid) {super (); this.views = new sparsearray& Lt View> (); This.convertview = view.inflate (context, layoutid, null); Convertview.settag (this);} /** * Get Holder * * @param context * @param convertviewgetview Convertview * @param layoutiditem View layout file ID * @ret Urn */public static ViewHolder2 getholder (context context, View Convertview,int LayoutID) {if (Convertview.gettag () = = nul L) {return new ViewHolder2 (context, layoutid);} Return (VIEWHOLDER2) Convertview.gettag ();} @SuppressWarnings ("unchecked") public <t extends view>t getchildview (int viewId) {View Childview = Views.get ( VIEWID); if (Childview = = null) {Childview = Convertview.findviewbyid (viewId); Views.put (ViewId, ChildviEW);} Return (T) Childview;} Public View Getconvertview () {return convertview;}}


Usage:

Public View GetView (int position, View Convertview, ViewGroup parent) {Viewholder Mviewholder = Viewholder.get (Context,co Nvertview,layoutid); TextView Price = Mviewholder.getview (R.id.price); Other Getviewreturn Mviewholder.getconvertview ();}


The first article also shared a framework:

Base-adapter-helper Click to open link


Temporary no time to study, interested students can look!


Engaged in Android development soon. There is a problem, I hope everyone message exchange, common growth!



Android Drip---viewholder universal, elegant writing

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.