Android Development, we can not leave adapter, each project has a lot of places need to adapter, then how do we let ourselves write adapter code less? That is the encapsulation adapter, let our adapter become omnipotent adapter. Below I listed my implementation of the idea, to provide you with learning and reference, of course, there is a better way to offer advice oh.
One, for the ListView or the GridView Universal Adapter (source)
1. The first step to achieve a common viewholder
/**
* Optimize the Viewholder for adapter
* Created by admin on 17/8/2.
*/
public class Viewholder {
View of current item
Private View Mcontentview;
Container for storing ID
Private sparsearray<view>mviews;
Public Viewholder (Context context, viewgroup parent, int position, @LayoutRes int layoutid) {
This.mviews = new sparsearray<> ();
This.mcontentview = Layoutinflater.from (context). Inflate (Layoutid,parent,false);
This.mContentView.setTag (this);
}
/**
* CREATE View
* @param context
* @param Contentview
* @param Parent
* @param position
* @param LayoutID
* @return
*/
public static Viewholder CreateView (Context context,view contentview,viewgroup parent,int position, @LayoutRes int LayoutID) {
if (contentview==null) {
return new Viewholder (Context,parent,position,layoutid);
}else {
Viewholder holder = (viewholder) contentview.gettag ();
return holder;
}
}
/**
* Provides view to external access
* @return
*/
Public View Getcontentview () {
return mcontentview;
}
/**
* Get control by ID
* @param ID
* @param <T>
* @return
*/
Public <t extends view> T Getviewbyid (@IdRes int id) {
View view = Mviews.get (ID);
if (view==null) {
View = Mcontentview.findviewbyid (ID);
Mviews.put (Id,view);
}
Return (T) view;
}
/**
* Here is the setting of the text of course also can expand more if you need to do it by yourself in this way.
* @param ID
* @param text
* @return
*/
Public Viewholder setText (@IdRes int id,charsequence text) {
TextView view = Getviewbyid (ID);
View.settext (text);
return this;
}
}
2. Second step to achieve common adapter
/**
* Public Adapter
* Created by admin on 17/8/2.
*/
Public abstract class Commonadapter<t> extends Baseadapter {
Private list<t>list;
Private context context;
private int layoutid;
Public Commonadapter (Context context,list<t>list,int layoutid) {
This.context=context;
This.list=list;
This.layoutid=layoutid;
}
@Override
public int GetCount () {
Return list==null? 0:list.size ();
}
@Override
Public Object getItem (int position) {
return List.get (position);
}
@Override
public long getitemid (int position) {
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Viewholder holder = Viewholder.createview (context,convertview,parent, Position, LayoutID);
This.onbind (Holder,list.get (position));
return Holder.getcontentview ();
}
protected abstract void Onbind (Viewholder holder,t model);
}
This will enable the adapter of the Almighty, and then we just need to inherit this universal adapter to easily bind the data.
3. Call
public class Mainactivity extends Appcompatactivity {
Private List<string>list=new arraylist<> ();
Private ListView Mlistview;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_butt_test);
This.mlistview = This.findviewbyid (R.id.listview)
for (int i=0;i<30;i++) {
List.add ("Test Data" + (i+1));
}
Mlistview.setadapter (New commonadapter<string> (This,list,r.layout.list_item) {
@Override
protected void Onbind (Viewholder holder, String model) {
It's OK to just bind the data here.
Holder.settext (R.id.title,model);
}
});
}
}
Android built ListView and GridView Universal Adapter adapter (with source)