RecyclerView.Adapter封装,最简单实用的BaseRecyclerViewAdapter;只需重写一个方法,设置数据链式调用;...

之前对ListView的BaseAdapter进行过封装,只需重写一个getView方法;

现在慢慢的RecyclerView成为主流,下面是RecyclerView.Adapter的封装;


BaseRecyclerViewHolder类:

public class BaseRecyclerHolder extends RecyclerView.ViewHolder {
    private SparseArrayCompat<View> mViews;
    public BaseRecyclerHolder(View itemView) {
        super(itemView);
        mViews = new SparseArrayCompat<>();
    }
    public <V extends View> V getView(@IdRes int res){
        View v = mViews.get(res);
        if (v == null){
            v = itemView.findViewById(res);
            mViews.put(res,v);
        }
        return (V)v;
    }
}

BaseRecyclerViewAdapter类:

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerHolder> {
    public Context context;
    public int layoutRes;
    public List<T> items;

    public BaseRecyclerAdapter(Context context,@LayoutRes int layoutRes){
      this.context = context;
      this.layoutRes = layoutRes;
      items = new ArrayList<T>();
    }

    public BaseRecyclerAdapter(Context context,@LayoutRes int layoutResId, @Nullable List<T> data) {
        this.context = context;
        this.items = data == null ? new ArrayList<T>() : data;
        if (layoutResId != 0) {
            this.layoutRes = layoutResId;
        }
    }

    @Override
    public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseRecyclerHolder(LayoutInflater.from(context).inflate(layoutRes,null));
    }

    @Override
    public void onBindViewHolder(BaseRecyclerHolder holder, int position) {
        convert(holder,position);
    }

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


    public void setData(List<T> items) {
        this.items = items == null ? new ArrayList<T>() : items;
        notifyDataSetChanged();
    }

    public void addData(@IntRange(from = 0) int position, @NonNull T data) {
        items.add(position, data);
        notifyItemInserted(position);
        compatibilityDataSizeChanged(1);
    }
    public void addData( @NonNull T data) {
        items.add(data);
        notifyItemInserted(items.size());
    }


    public void remove(int position) {
        items.remove(position);
        int internalPosition = position;
        notifyItemRemoved(internalPosition);
        notifyItemRangeChanged(internalPosition, items.size() - internalPosition);
    }

    private void compatibilityDataSizeChanged(int size) {
        final int dataSize = items == null ? 0 : items.size();
        if (dataSize == size) {
            notifyDataSetChanged();
        }
    }

    /**
     * 需要重写的方法
     * @param holder
     * @param position
     */
    public abstract void convert(BaseRecyclerHolder holder, int position);
}

如何使用:MainActivity类:

public class MainActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private List<String> d = Arrays.asList(
            "A","B","C","D","E","F","G"
            ,"H","I","J","K","L","M","N"
            ,"O","P","Q","R","S","T"
            ,"U","V","W","X","Y","Z");
    private RecyclerView rv ;
    private BaseRecyclerAdapter ap;
    private List<String> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
//        ap = new BaseRecyclerAdapter<String>(MainActivity.this, R.layout.item_rv){
//      还可以这样用
//            @Override
//            public void convert(BaseRecyclerHolder holder, int position) {
//
//            }
//        };
        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new GridLayoutManager(this,3));
        ap = new Ap(this,R.layout.item_rv);
        rv.setAdapter(ap);
        ap.setData(datas);
    }

    private void initData() {
        datas = new ArrayList<>();
        for (int i = 0; i < d.size(); i++) {
            datas.add(d.get(i));
        }
    }

    class Ap extends BaseRecyclerAdapter<String>{

        public Ap(Context c, int layoutRes) {
            super(c, layoutRes);
        }

        @Override
        public void convert(BaseRecyclerHolder holder, final int position) {
            //链式调用
              holder.setText(R.id.tv,items.get(position))
                    .setOnClickListener(R.id.tv, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ap.remove(position);
                        }
                    });
        }
    }

}

其实就只有这一个是你关注的,需要实现的Adapter:

class Ap extends BaseRecyclerAdapter<String>{

    public Ap(Context c, int layoutRes) {
        super(c, layoutRes);
    }

    @Override
    public void convert(BaseRecyclerHolder holder, final int position) {
        //链式调用
          holder.setText(R.id.tv,items.get(position))
                .setOnClickListener(R.id.tv, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ap.remove(position);
                    }
                });
    }
}

是不是超简单:

这个是Demo,里面封装的更加全面;点击打开链接

GitHub

转载于:https://www.cnblogs.com/cuichen16/p/10785994.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值