2015/10/10
一不小心在看ListView优化的时候看到了Adapter的复用,发现万能的Adapter发现确实很好,节省多余的代码在项目中
两个网址
http://www.cnblogs.com/smyhvae/p/4477079.html 代码基本来自这里
https://github.com/JoanZapata/base-adapter-helper github上面的挺有名的 稍微看了一下这两个代码差不多
这个源码就是这样
public abstract class MyBaseAdapter<T> extends BaseAdapter {
private List<T> mDatas;
private Context mContext;
private LayoutInflater mLayoutInflater;
private int layoutId;
public MyBaseAdapter(List<T> mDatas, Context mContext, int layoutId) {
this.mDatas = mDatas;
this.mContext = mContext;
this.mLayoutInflater = LayoutInflater.from(mContext);
this.layoutId = layoutId;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public T getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//初始化ViewHolder,使用通用的ViewHolder,一样代码就搞定ViewHolder的初始化咯
MyViewHolder holder = MyViewHolder.get(mContext, convertView, parent, layoutId, position);//layoutId就是单个item的布局
convert(holder, getItem(position));
return holder.getConvertView(); //这一行的代码要注意了
}
//将convert方法公布出去 子类实现
public abstract void convert(MyViewHolder holder, T t);
}
class MyViewHolder {
private SparseArray<View> mViews;
private int mPosition;
private View mCurrentView;
public MyViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
this.mPosition = position;
this.mViews = new SparseArray<View>();
mCurrentView = LayoutInflater.from(context).inflate(layoutId, parent, false);
mCurrentView.setTag(this);
}
public static MyViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new MyViewHolder(context, parent, layoutId, position);
} else {
MyViewHolder holder = (MyViewHolder) convertView.getTag();
holder.mPosition = position; //ViewHolder是复用的,position更新一下
return holder;
}
}
/*
通过viewId获取控件
*/
//使用的是泛型T,返回的是View的子类
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
//复用
if (view == null) {
view = mCurrentView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public View getConvertView() {
return mCurrentView;
}
}
在Activity中
public class BaseAdapterActivity extends ActionBarActivity {
private android.widget.ListView lvshow;
private android.widget.GridView gvshow;
private android.widget.Button btlistview;
private android.widget.Button btgridview;
public List<String> lists=new ArrayList<String>();
private GridViewAdpater gridViewAdpater;
private ListViewAdpater listViewAdpater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_adapter);
this.btgridview = (Button) findViewById(R.id.bt_gridview);
this.btlistview = (Button) findViewById(R.id.bt_listview);
this.gvshow = (GridView) findViewById(R.id.gv_show);
this.lvshow = (ListView) findViewById(R.id.lv_show);
listViewAdpater=new ListViewAdpater(lists,this,R.layout.item_baseadapter_listview);
lvshow.setAdapter(listViewAdpater);
btlistview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gvshow.setVisibility(View.GONE);
lvshow.setVisibility(View.VISIBLE);
lists.clear();
for(int i=0 ; i<200 ; i++) {
lists.add("Daemon " + i);
}
listViewAdpater.notifyDataSetChanged();
}
});
lists.clear();
gridViewAdpater=new GridViewAdpater(lists,this,R.layout.item_baseadapter_listview);
gvshow.setAdapter(gridViewAdpater);
btgridview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lvshow.setVisibility(View.GONE);
gvshow.setVisibility(View.VISIBLE);
lists.clear();
for(int i=0 ; i<200 ; i++){
lists.add("Daemon "+i);
}
gridViewAdpater.notifyDataSetChanged();
}
});
}
class ListViewAdpater extends MyBaseAdapter<String>{
public ListViewAdpater(List<String> mDatas, Context mContext, int layoutId) {
super(mDatas, mContext, layoutId);
}
@Override
public void convert(MyViewHolder holder, String s) {
((TextView)holder.getView(R.id.tv_show)).setText(s);
}
}
class GridViewAdpater extends MyBaseAdapter<String>{
public GridViewAdpater(List<String> mDatas, Context mContext, int layoutId) {
super(mDatas, mContext, layoutId);
}
@Override
public void convert(MyViewHolder holder, String s) {
((TextView)holder.getView(R.id.tv_show)).setText(s);
}
}
这样做到了Adapter的复用写来节省了很多代码,不用在每次都新建继承BaseAdpater写很多重复的代码了
效果图
GIF.gif
网友评论