Adapter universal adapter, only 3 lines, Adapter adapter 3 lines

Source: Internet
Author: User
Tags addall

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 );}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.