Android recyclerView的简单封装

1.编写BaseRvViewHolder

public class BaseRvViewHolder extends RecyclerView.ViewHolder {
    public SparseArray<View> arrayViews;
    public Context mContext;
    protected BaseRvViewHolder(Context context,View itemView) {
        super(itemView);
        arrayViews=new SparseArray<View>();
        mContext=context;
    }

    public  <T extends View> T getView(int viewId){
        View view = arrayViews.get(viewId);
        if (view==null){
            view=itemView.findViewById(viewId);
            arrayViews.put(viewId,view);
        }
        return (T)view;
    }
}

2.创建基类适配器

/**
 * Author:DoctorWei
 * Time:2018/12/5 18:34
 * Description:
 * email:1348172474@qq.com
 */

public abstract class BaseRvAdapter<T> extends RecyclerView.Adapter<BaseRvViewHolder> {
    private List<T> mDatas;
    protected Context mContext;
    private int layout;
    protected BaseRvAdapter(List<T> mDatas, Context mContext,int layout) {
        if (mDatas!=null) {
            this.mDatas = mDatas;
        }else{
            this.mDatas=new ArrayList<>();
        }
        this.mContext = mContext;
        this.layout=layout;
    }

    @NonNull
    @Override
    public BaseRvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflateView = LayoutInflater.from(mContext).inflate(layout, parent, false);
        BaseRvViewHolder normalRvViewHolder=new BaseRvViewHolder(mContext,inflateView);
        return normalRvViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseRvViewHolder holder, final int position) {
        setListener(holder, position);
        bindDatas(holder,mDatas.get(position),position);
    }

    private void setListener(@NonNull BaseRvViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(position);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemLongClickListener.onItemLongClick(position);
                return true;
            }
        });
    }

    protected abstract void bindDatas(BaseRvViewHolder holder, T t,int position);
    @Override
    public int getItemCount() {
        return mDatas.size();
    }
    public List<T> getItems(){
        return mDatas;
    }
    public void refreshDatas(List<T> refreshDatas){
        if (mDatas!=null){
            mDatas.clear();
            mDatas.addAll(refreshDatas);
            notifyDataSetChanged();
        }
    }
    public void loadDatas(List<T> loadDatas){
        if (mDatas!=null){
            mDatas.addAll(loadDatas);
            notifyDataSetChanged();
        }
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }
    public OnItemClickListener onItemClickListener;
    public interface OnItemClickListener{
        void onItemClick(int position);
    }
    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener){
        this.onItemLongClickListener=onItemLongClickListener;
    }
    public OnItemLongClickListener onItemLongClickListener;
    public interface OnItemLongClickListener{
        void onItemLongClick(int position);
    }
}
3.构建自己的适配器
public class FunctionAdapter extends BaseRvAdapter<VideoRes.DataBean> {
    public FunctionAdapter(List mDatas, Context mContext, int layout) {
        super(mDatas, mContext, layout);
    }


    @Override
    protected void bindDatas(BaseRvViewHolder holder, VideoRes.DataBean dataBean, int position) {
        ImageView view = holder.getView(R.id.iv_function);
        GlideUtil.loadDefaultUrl(mContext,dataBean.getThumbnail(),view);
    }
}

4.初始化适配器
mRvFunction = (RecyclerView) view.findViewById(R.id.rv_function);
functionAdapter=new FunctionAdapter(null,_mActivity,R.layout.function_item_layout); mRvFunction.setAdapter(functionAdapter);

5.更新数据
functionAdapter.refreshDatas(newDatas);

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值