android 手写万能adapter适配器

android开发中,我们离不开adapter,每个项目都有很多地方需要adapter,那么我们如何让自己少写adapter代码呢?那就是封装adapter,让我们的adapter成为万能的adapter。下面我列出我实现的思路,供给大家学习与参考,当然有更好的方式也可以提供建议哦。

一、针对Listview或GridView的万能适配器(源码)

 

1.第一步实现公用的viewholder
/**
* 优化adapter用的ViewHolder
* Created by admin on 17/8/2.
*/

public class ViewHolder {

//当前item的view
private View mContentView;
// 用于存储id用的容器
private SparseArray<View>mViews;

public ViewHolder(Context context, ViewGroup parent, int position, @LayoutRes int layoutId){


this.mViews = new SparseArray<>();
this.mContentView = LayoutInflater.from(context).inflate(layoutId,parent,false);
this.mContentView.setTag(this);
}

/**
* 创建view
* @param context
* @param contentView
* @param parent
* @param position
* @param layoutId
* @return
*/
public static ViewHolder createView(Context context,View contentView,ViewGroup parent,int position,@LayoutRes int layoutId){

if(contentView==null){

return new ViewHolder(context,parent,position,layoutId);

}else {

ViewHolder holder = (ViewHolder) contentView.getTag();

return holder;
}

}

/**
* 提供view给外部访问
* @return
*/

public View getContentView() {
return mContentView;
}


/**
* 通过id获取控件
* @param id
* @param <T>
* @return
*/
public <T extends View> T getViewById(@IdRes int id){


View view = mViews.get(id);
if(view==null){

view = mContentView.findViewById(id);
mViews.put(id,view);
}

return (T)view;

}

/**
* 这里是设置文字当然还可以扩展更多如果有需要自己按这种方式实现一下就可以的
* @param id
* @param text
* @return
*/
public ViewHolder setText(@IdRes int id,CharSequence text){

TextView view = getViewById(id);
view.setText(text);
return this;
}

}

2.第二步实现公用的adapter
/**
* 公用adapter
* Created by admin on 17/8/2.
*/

public abstract class CommonAdapter<T> extends BaseAdapter {

private List<T>list;
private Context context;
private int layoutId;
public CommonAdapter(Context context,List<T>list,int layoutId){

this.context=context;
this.list=list;
this.layoutId=layoutId;
}

@Override
public int getCount() {
return list==null? 0:list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = ViewHolder.createView(context,convertView,parent, position, layoutId);
this.onBind(holder,list.get(position));
return holder.getContentView();
}

protected abstract void onBind(ViewHolder holder,T model);
}

这样就可以实现万能的adapter了,接下来我们就只需要继承这个万能适配器就可以了轻松绑定数据了。
3.调用
public class MainActivity extends AppCompatActivity {
    private List<String>list=new ArrayList<>();

private ListView mListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_butt_test);
       this.mListView = this.findViewById(R.id.listView)

for(int i=0;i<30;i++){

list.add("测试数据"+(i+1));
}


mListView.setAdapter(new CommonAdapter<String>(this,list,R.layout.list_item) {
@Override
protected void onBind(ViewHolder holder, String model) {
           //只需在这里绑定数据就ok了
               holder.setText(R.id.title,model);

}
});
}
}

posted @ 2017-08-02 19:25  猿哥爱码  阅读(232)  评论(0编辑  收藏  举报