对系统的BaseAdapter进一步抽象和封装:
- 通用数据适配器类
/***********
* @Author rape flower
* @Date 2017-12-04 16:48
* @Describe 通用数据适配器类
*/
public abstract class AbstractBaseAdapter<T> extends BaseAdapter {
protected Context mContext;
protected List<T> mData;
protected LayoutInflater mInflater;
public AbstractBaseAdapter(Context context, List<T> data) {
this.mContext = context;
mInflater = LayoutInflater.from(context);
this.mData = data;
}
@Override
public int getCount() {
return mData == null ? 0 : mData.size();
}
@Override
public T getItem(int position) {
return mData == null ? (T) null : mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AbstractViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(onBindLayout(), parent, false);
holder = onCreateViewHolder();
if (holder == null) {
throw new NullPointerException("The view holder object is null, you must instance view holder.");
}
holder.initWidget(convertView);
convertView.setTag(holder);
} else {
holder = (AbstractViewHolder) convertView.getTag();
}
onBindViewHolder(holder, getItem(position), position);
return convertView;
}
public void setVisible(View view) {
if (view == null) return;
if (view.getVisibility() == View.GONE || view.getVisibility() == View.INVISIBLE) {
view.setVisibility(View.VISIBLE);
}
}
public void setGone(View view) {
if (view == null) return;
if (view.getVisibility() == View.VISIBLE) {
view.setVisibility(View.GONE);
}
}
public void setInvisible(View view) {
if (view == null) return;
if (view.getVisibility() == View.VISIBLE) {
view.setVisibility(View.INVISIBLE);
}
}
public abstract int onBindLayout();
public abstract void onBindViewHolder(AbstractViewHolder holder, T data, int position);
public abstract AbstractViewHolder onCreateViewHolder();
}
- ViewHolder基类
/***********
* @Author rape flower
* @Date 2017-12-04 16:48
* @Describe 所有自定义的View Holder需要继承自此ViewHolder
*/
public abstract class AbstractViewHolder {
public abstract void initWidget(View view);
}
-
使用
public class GoodsStockAdapter extends AbstractBaseAdapter<Goods> {
public GoodsStockAdapter(Context context, List<Goods> data) {
super(context, data);
}
@Override
public int onBindLayout() {
return R.layout.item_goods_stock;
}
@Override
public void onBindViewHolder(AbstractViewHolder holder, Goods data, int position) {
GoodsStockHolder goodsStockHolder = (GoodsStockHolder) holder;
goodsStockHolder.tvGoodsName.setText(data.getGoodsName());
goodsStockHolder.tvTobePurchased.setText(data.getGoodsTobePurchasedNumber()+"");
goodsStockHolder.tvPurchased.setText(data.getGoodsPurchasedNumber()+"");
goodsStockHolder.tvSold.setText(data.getGoodsSoldNumber()+"");
}
@Override
public AbstractViewHolder onCreateViewHolder() {
return new GoodsStockHolder();
}
private class GoodsStockHolder extends AbstractViewHolder {
TextView tvGoodsName;
TextView tvTobePurchased;
TextView tvPurchased;
TextView tvSold;
@Override
public void initWidget(View view) {
tvGoodsName = (TextView) view.findViewById(R.id.tv_goods_name);
tvTobePurchased = (TextView) view.findViewById(R.id.tv_to_be_purchased);
tvPurchased = (TextView) view.findViewById(R.id.tv_purchased);
tvSold = (TextView) view.findViewById(R.id.tv_sold);
}
}
}
网友评论