BaseAdapter的抽取
原型
一般继承自BaseAdaper主要重写getView()和getCount()方法,在getView方法中创建ViewHolder类(RecyclerView自带ViewHolder,而ListView需要我们自己实现)避免重复调用findViewById。
这是一个典型的例子:
package rc.loveq.listviewadapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Author:Rc
* Csdn:http://blog.csdn.net/loveqrc
* 0n 2016/12/16 08:55
* Email:664215432@qq.com
*/
public class MyBaseAdapter extends BaseAdapter {
private List<Product> mProducts;
public MyBaseAdapter(List<Product> products) {
mProducts = products;
}
@Override
public int getCount() {
return mProducts == null ? 0 : mProducts.size();
}
@Override
public Object getItem(int position) {
return mProducts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.item_product_list, null);
holder=new ViewHolder(convertView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.mId.setText(mProducts.get(position).id);
holder.mMemberNum.setText(mProducts.get(position).memberNum);
holder.mMoney.setText(mProducts.get(position).money);
holder.mName.setText(mProducts.get(position).name);
return convertView;
}
}
class ViewHolder {
@BindView(R.id.name)
TextView mName;
@BindView(R.id.memberNum)
TextView mMemberNum;
@BindView(R.id.money)
TextView mMoney;
@BindView(R.id.id)
TextView mId;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
优化第一步–泛型的引入
从上面可以看出每次继承自BaseAdapter其实就是实现那几个方法,而每次重写
差别都不会大,这时候我们可以引入泛型进行第一步封装,至于具体的getView()方法抽取
成抽象的方法,让由子类具体去实现。
package rc.loveq.listviewadapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* Author:Rc
* Csdn:http://blog.csdn.net/loveqrc
* 0n 2016/12/16 09:41
* Email:664215432@qq.com
*/
public abstract class MyBaseAdapter2<T> extends BaseAdapter {
private List<T> mList;
public MyBaseAdapter2(List<T> list) {
mList = list;
}
@Override
public int getCount() {
return mList==null?0:mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getYourView(position,convertView,parent);
}
protected abstract View getYourView(int position, View convertView, ViewGroup parent);
}
通过第一步抽取之后,我们的代码简洁了很多,只要重写getYourView方法即可。
mLv.setAdapter(new MyBaseAdapter2<Product>(mList) {
@Override
protected View getYourView(int position, View convertView, ViewGroup parent) {
return null;
}
});
优化第二步–ViewHolder的抽取
在第二步中,我们还是要自己创建ViewHolder,那有没有什么办法在父类里面做呢?
package rc.loveq.listviewadapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
/**
* Author:Rc
* Csdn:http://blog.csdn.net/loveqrc
* 0n 2016/12/16 11:26
* Email:664215432@qq.com
*/
public abstract class MyBaseAdapter3<T> extends BaseAdapter {
private List<T> mList;
public MyBaseAdapter3(List<T> list) {
mList = list;
}
@Override
public int getCount() {
return mList==null?0:mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
BaseHolder holder=null;
if (convertView == null) {
holder=getHolder();
}else{
holder = (BaseHolder) convertView.getTag();
}
holder.onBindView(mList.get(position));
return holder.mRootView;
}
protected abstract BaseHolder getHolder();
}
父类不知道怎么做的,弄成抽象方法,交由子类具体去实现。
package rc.loveq.listviewadapter;
import android.view.View;
import butterknife.ButterKnife;
/**
* Author:Rc
* Csdn:http://blog.csdn.net/loveqrc
* 0n 2016/12/16 11:28
* Email:664215432@qq.com
*/
public abstract class BaseHolder<T> {
public View mRootView;
public BaseHolder() {
mRootView = initView();
mRootView.setTag(this);
ButterKnife.bind(this,mRootView);
}
protected abstract View initView();
public abstract void onBindView(T t);
}
使用
mLv.setAdapter(new MyBaseAdapter3<Product>(mList) {
@Override
protected BaseHolder getHolder() {
return new MyViewHolder();
}
});
}
class MyViewHolder extends BaseHolder<Product> {
@BindView(R.id.name)
TextView mName;
@BindView(R.id.memberNum)
TextView mMemberNum;
@BindView(R.id.money)
TextView mMoney;
@BindView(R.id.id)
TextView mId;
@Override
protected View initView() {
View view = View.inflate(MainActivity.this, R.layout.item_product_list, null);
return view;
}
@Override
public void onBindView(Product product) {
mId.setText(product.id);
mMemberNum.setText(product.memberNum);
mMoney.setText(product.money);
mName.setText(product.name);
}
}