Adapter universal adapter, only 3 lines, Adapter adapter 3 lines
Not to mention anything. First, an anonymous internal class, one adapter + two listview Optimizations
Note: holder. setText () is to extract the view operation. If you need to operate imageview, add setImageView.
So easy!
To be as simple as the code above, OK imports two of my classes
As for how to write the two classes, you should understand it after a little time.
Class 1 ViewHodler. java
Package com. zhuimore. iqzone. adapter. base; import android. content. context; import android. util. sparseArray; import android. view. view; import android. widget. textView; public class ViewHolder {private View mConvertView; // SparseArray is more efficient than map when key and valse are int and Obj. private SparseArray <View> mViews = new SparseArray <View> (); public ViewHolder () {} public void init (Context context, int layoutId, View convertView) {if (convertView = null) {convertView = View. inflate (context, layoutId, null); convertView. setTag (this); this. mConvertView = convertView;} public View getView (int viewId) {View v = mViews. get (viewId); if (v = null) {v = mConvertView. findViewById (viewId); mViews. put (viewId, v);} return v;} public View getConvertView () {return mConvertView;} public ViewHolder setText (int viewId, String content) {TextView v = (TextView) getView (viewId); v. setText (content); return this ;}}
Class 2 AdapterBestBase. java
Package com. zhuimore. iqzone. adapter. base; import java. util. list; import android. content. context; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; public abstract class AdapterBestBase <T> extends BaseAdapter {public Context mContext; public List <T> mList; public ViewHolder holder; public int layoutId; public AdapterBestBase (Context context, int layoutId, list <T> mList) {this. mContext = context; this. mList = mList; this. layoutId = layoutId; // One adapter, one holderholder = new ViewHolder ();} public List <T> getList () {return mList;} public void appendToList (List <T> list) {if (list = null) {return;} mList. addAll (list); notifyDataSetChanged ();} public void appendToTopList (List <T> list) {if (list = null) {return;} mList. addAll (0, list); notifyDataSetChanged ();} public void appendT (T object) {if (object = null) {return;} mList. add (object); notifyDataSetChanged ();} public void removeT (T object) {if (object = null) {return;} mList. remove (object); notifyDataSetChanged ();} public void clear () {mList. clear (); notifyDataSetChanged () ;}@ Overridepublic int getCount () {return mList. size () ;}@ Overridepublic T getItem (int position) {if (position> mList. size ()-1) {return null;} return mList. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// detect convertViewholder. init (mContext, layoutId, convertView); // callback getExView (holder, getItem (position); return holder. getConvertView ();} protected abstract void getExView (ViewHolder holder, T t );}