In Android development, the ListView is heavily used and the ListView needs a variety of situations. Sometimes you just need a simple text list, sometimes you need a more complex layout as the content of the item, and sometimes you need different types, and the contents of different layouts are staggered in the ListView. The usual ListView optimization strategy uses Convertview and Viewholder to reuse item to enhance the efficiency of the ListView. However, I may need to write a new adapter for each ListView that has a different layout. If you also want to show a different layout in a ListView, you can generate a lot of duplicate code. Therefore, here I would like to change a train of thought, for the moment has not been enough practice test, it may be wrong, I just record the idea here.
First, what does the adapter of each ListView really need to enter? Actually there are only two, one is the layout, the other is the data. Adapter's job is to connect the two. Therefore, I want only the connected content in adapter. How the layout is implemented should be written in the layout (because there may be multiple layouts mixed in a ListView).
Next, the type of the data is unknown, we only know it should be a list, and then what is included in the list, we don't need to know, each layout to go to the list to find what they need.
Then, each layout should have some method that is public and must be implemented. For example: loading a layout file, getting controls in the layout, extracting information from the data to update the layout.
Finally, adapter only needs to tell the layout in GetView, you can now go to the Positon data list to update the layout.
Here is the code
Layout class
Baseadapterviewholder:
Public Abstract classBaseadapterviewholder {Context mcontext; List<?>mdatalist; View Mconvertview; PublicBaseadapterviewholder (context context, list<?> dataList,intConvertviewid) {Mcontext=context; Mdatalist=dataList; Mconvertview= Layoutinflater.from (context). Inflate (Convertviewid,NULL); } PublicView Getconvertview () {returnMconvertview; } Abstract Public voidGetitemchildview (View convertview); Abstract Public voidSetitemchlidview (intposition);}
This class is a virtual class, in this case the implementation example of a subclass
Public classDoufuitem2dataextendsBaseadapterviewholderImplementsView.onclicklistener {ImageView userimage; TextView itemState; TextView Itemupdatetime; intMpositon; PublicDoufuitem2data (context context, list<?>dataList) { Super(context, dataList, r.layout.doufu_itemview_test); } Public voidGetitemchildview (View mconvertview) {userimage=(ImageView) Mconvertview.findviewbyid (r.id.doufu_item_user_image); ItemState=(TextView) Mconvertview.findviewbyid (r.id.doufu_item_state); Itemupdatetime=(TextView) Mconvertview.findviewbyid (r.id.doufu_item_update_time); } Public voidSetitemchlidview (intposition) {Mpositon=position; Itemstate.settext ("End" +Mpositon); Itemupdatetime.settext ("Update Just"); Userimage.setonclicklistener ( This); } @Override Public voidOnClick (View v) {toast.maketext (Mcontext, Mdatalist.get (Mpositon). toString ()+Mpositon, Toast.length_short). Show (); }}
Finally, attach the use method in the adapter of this layout class:
Public classDoufufragmentlistadapterextendsBaseadapter {PrivateContext Mcontext; PrivateList<?>mdatalist; PublicDoufufragmentlistadapter (context context, list<?>dataList) {Mcontext=context; Mdatalist=dataList; } @Override Public intGetCount () {returnmdatalist.size (); } @Override PublicObject GetItem (intposition) { return NULL; } @Override Public LongGetitemid (intposition) { return0; } @Override PublicView GetView (Final intposition, View Convertview, ViewGroup parent) {Baseadapterviewholder viewholder; if(Convertview = =NULL) {Viewholder=NewDoufuitem2data (Mcontext, mdatalist); Convertview=Viewholder.getconvertview (); Viewholder.getitemchildview (Convertview); Convertview.settag (Viewholder); } Else{Viewholder=(Doufuitem2data) Convertview.gettag (); } viewholder.setitemchlidview (position); returnConvertview; }}
When we need to use different layouts, we only need to Viewholder = new Doufuitem2data (Mcontext, mdatalist); Change to another layout class.
done!
Optimization attempt of the ListView