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