1. 程式人生 > >Android recyclerView的簡單封裝

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:[email protected]
 */

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