寫個繫結ButterKnife的BaseAdapter基礎類別

2020-09-27 08:00:55

   抽取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;
  • getView 
 @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;
    }
  •    子類處理頁面資料方法initData
public abstract void initData(Context context, int i, ViewGroup viewGroup, List<T> mList, ViewHolder viewHolder);
  •  getViewHolder需要子類重寫
public abstract  ViewHolder  getViewHolder();
  •   ViewHolder基礎類別 主要做繫結ButterKnife
public abstract class ViewHolder {
      public void ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
  • 子類只需要繼承BaseAdapter 跟ViewHolder 定義好資料泛型即可用

列如:

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);
        }
    }

}