针对recyclerView的adapter封装

public abstract class RecycleBaseAdapter<T, K extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected Context mContext;
    protected List<T> dataList;
    protected LayoutInflater mInflater;
    protected RecycleViewAdapterListner<T> listner;

    public RecycleBaseAdapter(Context mContext, List<T> dataList, RecycleViewAdapterListner<T> listner) {
        this.mContext = mContext;
        this.dataList = dataList;
        this.listner = listner;
        mInflater = LayoutInflater.from(mContext);
    }
    public RecycleBaseAdapter(Context mContext, List<T> dataList) {
        this.mContext = mContext;
        this.dataList = dataList;
        this.listner = listner;
        mInflater = LayoutInflater.from(mContext);
    }

    protected View inFlaterView(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        return mInflater.inflate(resource, root, attachToRoot);
    }

    @Override
    public K onCreateViewHolder(ViewGroup parent, int viewType) {
        return createViewholder(parent, viewType);
    }

    private T getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        bindViewholder(getItem(position), (K) holder, position);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public abstract K createViewholder(ViewGroup parent, int viewType);

    public abstract void bindViewholder(T t, K holder, int position);
}


public abstract class RecycleViewAdapterListner<T> implements RecycleViewAdapter<T>{
    @Override
    public void viewOnclick1(View view, int position, T t) {

    }

    @Override
    public void viewOnclick2(View view, int position, T t) {

    }

    @Override
    public void viewOnclick3(View view, int position, T t) {

    }

    @Override
    public void viewOnclick4(View view, int position, T t) {

    }
}

public interface RecycleViewAdapter<T> {
    void viewOnclick1(View view, int position, T t);

    void viewOnclick2(View view, int position, T t);

    void viewOnclick3(View view, int position, T t);

    void viewOnclick4(View view, int position, T t);
}


用法:

class MainIndexAdapter4 extends RecycleBaseAdapter<MainIndexBean.MainIndexInfo, MainIndexAdapter4.ViewHolder> {

            public MainIndexAdapter4(Context mContext, List<MainIndexBean.MainIndexInfo> dataList) {
                super(mContext, dataList);
            }

            @Override
            public ViewHolder createViewholder(ViewGroup parent, int viewType) {
                return new ViewHolder(inFlaterView(R.layout.report_time_recycle_adapter_layout4, parent, false));
            }

            @Override
            public void bindViewholder(MainIndexBean.MainIndexInfo mainIndex, ViewHolder holder, int position) {
                holder.ROER.setText(mainIndex.getROER());
            }

            class ViewHolder extends RecyclerView.ViewHolder {
                @Bind(R.id.ROER)
                TextView ROER;

                public ViewHolder(View itemView) {
                    super(itemView);
                    ButterKnife.bind(this, itemView);
                }
            }
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值