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