万能Adapter以及ViewHolder性能优化

//CommonAdapter

import
android.content.Context; import android.widget.BaseAdapter; import java.util.List; /** * Created by Administrator on 2016/1/28. */ public abstract class CommonAdapter<T> extends BaseAdapter { private List<T> mDatas; private int LayoutId; private Context context; public CommonAdapter(List mDatas,int layoutId,Context context){ this.mDatas=mDatas; this.context=context; this.LayoutId=layoutId; } @Override public long getItemId(int position) { return position; } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public int getCount() { return mDatas.size(); } public List<T> getList(){ return mDatas; } public void addBeanAtStart(T data){ mDatas.add(0,data); notifyDataSetChanged(); } public void addBeanAtEnd(T data){ mDatas.add(data); notifyDataSetChanged(); } public void addListAtStart(List datas){ mDatas.addAll(0, datas); notifyDataSetChanged(); } public void addListAtEnd(List datas){ mDatas.addAll(datas); notifyDataSetChanged(); } public void remove(int position){ if (mDatas!=null&&position<mDatas.size()&&position>=0) mDatas.remove(position); notifyDataSetChanged(); } public void remove(T data){ mDatas.remove(data); notifyDataSetChanged(); } public void removeAll(){ if(mDatas!=null){ mDatas.clear(); notifyDataSetChanged(); } } public void replaceBean(int position,T data){ mDatas.remove(position); mDatas.add(position,data); } }
ViewHolder :
import android.util.SparseArray;
import android.view.View;

/**
 * Created by Administrator on 2016/1/28.
 */
public class ViewHolder {
    public static <T extends View> View get(int id, View convertView){
        SparseArray<View> viewHolder= (SparseArray<View>) convertView.getTag();
        if (viewHolder==null){
            viewHolder=new SparseArray<>();
            convertView.setTag(viewHolder);
        }
        View childView=viewHolder.get(id);
        if(childView==null){
            childView=convertView.findViewById(id);
            viewHolder.put(id, childView);
        }
        return childView;
    }
}

 

posted @ 2016-01-28 22:03  戎码之路  阅读(495)  评论(0编辑  收藏  举报