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 these, 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 self-organized, save it, the province forget, by the way share!


The second kind of elegant writing in a little bit of a small problem, it should be the author careless!


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;}}

How to use:

@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;}

The second type:
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< view> views;private View convertview;private ViewHolder2 (view Convertview) {super (); this.views = new sparsearray< View> (); This.convertview = Convertview;convertview.settag (this);} public static ViewHolder2 Getholder (View convertview) {if (Convertview.gettag () = = null) {<span style= "White-space: Pre "></span>//here should be the author careless write wrong, if the view is the original article remember to modify return new ViewHolder2 (Convertview);} 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;}}


How to use:

Public View GetView (int position, View Convertview, ViewGroup parent) {if (Convertview = = null) {Convertview = Layoutinfla Ter.from (context). Inflate (R.layout.good_list_item, NULL, FALSE);} Viewholder Mviewholder = Viewholder.get (Convertview); TextView Price = Mviewholder.getview (R.id.price); Other Getviewreturn Convertview;}


The first article also shared a framework:

Base-adapter-helper Click to open link


Temporarily no time to study, interested students can look!


Engaged in the development of Android 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.