RecyclerView万能适配器

1.先写一个BaseAdapter

public abstract class BaseRecyclerViewAdapter extends RecyclerView.Adapter {
    protected List tList;
    protected Context mContext;
    protected LayoutInflater mLayoutInflater;
    protected int layId;

    private OnItemClickListener mOnItemClickListener;

    public BaseRecyclerViewAdapter(List tList, Context mContext, int layId) {
        this.tList = tList;
        this.mContext = mContext;
        this.layId=layId;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(layId, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(final BaseRecyclerViewAdapter.ViewHolder holder, final int position) {
        convert(holder,tList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClick(position,holder);
            }
        });

    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;

    }

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

    public abstract  void convert(ViewHolder holder,T itemData);


    public class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);

        }

    }

     /**
     * 点击事件接口
     */
    public interface OnItemClickListener{
        void onItemClick(int position,BaseRecyclerViewAdapter.ViewHolder viewHolder);
    }



}

2.写一个adapter 继承上面写的Base

public class RecyclerAdapter extends BaseRecyclerViewAdapter {
    public RecyclerAdapter(List lists, Context mContext, int layId, RecyclerView recyclerView) {
        super(lists, mContext, layId);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
    }
    @Override
    public void convert(ViewHolder holder, String itemData) {
        ((TextView)holder.itemView.findViewById(R.id.text)).setText(itemData);
    }
}

3.然后再函数中传入布局使用使用

recler = findViewById(R.id.recler);
        List Str = new ArrayList();
        for (int i = 0; i <10 ; i++) {
            Str.add("10");
        }
        RecyclerAdapter recyclerAdapter = new RecyclerAdapter(Str, this, R.layout.item_recycler, recler);
        recler.setAdapter(recyclerAdapter);

        recyclerAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, BaseRecyclerViewAdapter.ViewHolder viewHolder) {
//                viewHolder.setText(R.id.text,"9");
            }
        });

 

4.下面我们可以对ViewHolder进行一些优化

public class ViewHolder extends RecyclerView.ViewHolder {

        private SparseArray mViewArray;

        public ViewHolder(View itemView) {
            super(itemView);
            mViewArray=new SparseArray<>();
        }

        /**
         * 通过id得到view
         * @param viewId
         * @param 
         * @return
         */
        public V getViewAtId(int viewId){
            View view = mViewArray.get(viewId);
            if(view==null){
                view=itemView.findViewById(viewId);
                mViewArray.put(viewId,view);
            }
            return (V)view;
        }

        /**
         * 设置文字
         * @param viewId
         * @param text
         * @return
         */
        public ViewHolder setText(int viewId,CharSequence text){
           TextView textView= getViewAtId(viewId);
           textView.setText(text);
           return  this;
        }

        /**
         * 通过网址设置图片
         * @param viewId
         * @param imgUrl
         * @return
         */
        public ViewHolder setImageFromUrl(int viewId,String imgUrl){
           ImageView imageView= getViewAtId(viewId);
            Glide.with(imageView.getContext()).load(imgUrl).into(imageView);
            return this;
        }

        /**
         * 通过资源来设置图片资源
         * @param viewId
         * @param resId
         * @return
         */
        public ViewHolder setImageFromRes(int viewId,int resId){
            ImageView imageView = getViewAtId(viewId);
            imageView.setImageResource(resId);
            return this;
        }

        /**
         * 设置控件透明度
         * @param viewId
         * @param visibility
         * @return
         */
        public ViewHolder setViewVisibility(int viewId,int visibility){
            View view = getViewAtId(viewId);
            view.setVisibility(visibility);
            return this;
        }


    }

4.1  RecyclerAdapter就可以这么写了

 @Override
    public void convert(final ViewHolder holder, final String itemData) {
        holder.setText(R.id.text,itemData);
    }

 

 

5.多布局接口的实现   在BaseRecyclerViewAdapter里面 添加

private MultiTypeSupport multiTypeSupport;
    /**
     * 多布局接口
     * @param 
     */
    public interface  MultiTypeSupport{
        int getLayoutId(T item);
    }
    public void setMultiTypeSupport(MultiTypeSupport multiTypeSupport){
        this.multiTypeSupport=multiTypeSupport;
    }


    @Override
    public int getItemViewType(int position) {
        if (multiTypeSupport!=null){
            return multiTypeSupport.getLayoutId(tList.get(position));
        }
        return super.getItemViewType(position);
    }

 

6.在主函数里面进行数据判断来切换布局

recyclerAdapter.setMultiTypeSupport(new BaseRecyclerViewAdapter.MultiTypeSupport() {
            @Override
            public int getLayoutId(String item) {
                if(item.equals("horizontalScrollCard")){
                    return R.layout.item_recyclerview_clidfragment;
                }else if(item.equals("header5")){
                    return R.layout.item_recyclerview_nofragment;
                }


                return 0;
            }
        });

最后一个问题,我们应该怎么在convert方法中实现对不同的布局进行不同的数据加载呢?毕竟我们这个方法的传入参数可没有ViewType。
我们可以在直接通过item数据来判断就可以了。

这个适配器不能用于ViewPager  不然他不会测量高度

你可能感兴趣的:(RecyclerView万能适配器)