Android Adapter缓存

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;

public abstract class AbsBaseAdapter extends BaseAdapter {
    protected List mList;
    protected Context context;
    public AbsBaseAdapter(Context context, List list) {
        mList = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public T getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    static public class ViewHolder {
        private final SparseArray views;
        private View convertView;
        
        private ViewHolder(Context context, View convertView, ViewGroup parent, int layoutId) {
            views = new SparseArray();
            this.convertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
            this.convertView.setTag(this);
        }
        private ViewHolder(View v){
            views = new SparseArray();
            this.convertView = v;
        }
        static public ViewHolder getInstance(Context context, View convertView, ViewGroup parent, int layoutId){
            if(convertView==null){
                ViewHolder holder = new ViewHolder(context, convertView, parent, layoutId);
                return holder;
            }else{
                return (ViewHolder) convertView.getTag();
            }
        }
        static public ViewHolder getInstance(View v){
                ViewHolder holder = new ViewHolder(v);
                return holder;
        }
        public View getView(){
            return convertView;
        }

        @SuppressWarnings("unchecked")
        public  T getView(int viewId) {
            View view = views.get(viewId);
            if (view == null) {
                view = convertView.findViewById(viewId);
                views.put(viewId, view);
            }
            return (T) view;
        }
    }
}

你可能感兴趣的:(Android Adapter缓存)