抽取baseAdapter基礎類別
public BaseAdapter(Context context, @LayoutRes int layout , List<T> List){
mContext=context;
mList = List;
mLayout = layout;
notifyDataSetChanged();
}
layout xml檢視 list後臺介面資料,
private Context mContext;
private List<T> mList;
private ViewHolder viewHolder;
private @LayoutRes int mLayout;
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (view == null) {
view = View.inflate(mContext, mLayout, null);
viewHolder = getViewHolder();
viewHolder.ViewHolder(view);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
initData(mContext,i, viewGroup, mList, viewHolder);
return view;
}
public abstract void initData(Context context, int i, ViewGroup viewGroup, List<T> mList, ViewHolder viewHolder);
public abstract ViewHolder getViewHolder();
public abstract class ViewHolder {
public void ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
列如:
public class AddWalletCreateAdapter extends BaseAdapter<CoinBean.ContentBean> {
@Override
public void initData(Context context, int i, ViewGroup viewGroup, List<CoinBean.ContentBean> mList, ViewHolder viewHolder) {
final AddWalletCreateHolder addWalletHolder= (AddWalletCreateHolder) viewHolder;
Glide.with(context).load(mList.get(i).getIcon()).into(addWalletHolder.iv_icon);
addWalletHolder.tv_name.setText(mList.get(i).getCoinName());
addWalletHolder.tv_full_name.setText(mList.get(i).getFullName());
}
@Override
public ViewHolder getViewHolder() {
return new AddWalletCreateHolder();
}
class AddWalletCreateHolder extends ViewHolder{
@BindView(R.id.iv_icon)
ImageView iv_icon;
@BindView(R.id.tv_name)
TextView tv_name;
@BindView(R.id.tv_full_name)
TextView tv_full_name;
}
}
使用 :
new AddWalletCreateAdapter().initView(this, R.layout.list_wallet_create, mContentList);
至此簡單的基礎類別完成了. 可以根據業務不同抽取更多二級基礎類別.
貼上基礎類別完整程式碼
public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {
private Context mContext;
private List<T> mList;
private ViewHolder viewHolder;
private @LayoutRes int mLayout;
@Override
public int getCount() {
if(mList==null){
return 0;
}
return mList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
public BaseAdapter(Context context, @LayoutRes int layout , List<T> List){
mContext=context;
mList = List;
mLayout = layout;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (view == null) {
view = View.inflate(mContext, mLayout, null);
viewHolder = getViewHolder();
viewHolder.ViewHolder(view);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
initData(mContext,i, viewGroup, mList, viewHolder);
return view;
}
public abstract void initData(Context context, int i, ViewGroup viewGroup, List<T> mList, ViewHolder viewHolder);
public abstract ViewHolder getViewHolder();
public abstract class ViewHolder {
public void ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}