赞
踩
首先是BaseAdapter,泛型里面传入数据类型和ViewBinding
- public abstract class BaseAdapter<T,V extends ViewBinding> extends RecyclerView.Adapter<BaseHolder> {
- private List<T> data;
-
- public BaseAdapter(List<T> data) {
- this.data = data;
- }
- @NonNull
- @Override
- public BaseHolder<V> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
- return new BaseHolder(onBindingView(viewGroup));
- }
-
- @Override
- public void onBindViewHolder(@NonNull BaseHolder holder, int position) {
- onBindingData(holder,data.get(position),position);
- }
-
- protected abstract void onBindingData(BaseHolder<V> holder, T t, int position);
- protected abstract V onBindingView(ViewGroup viewGroup);
-
- @Override
- public int getItemCount() {
- return data==null?0:data.size();
- }
- }
BaseHolder
- public class BaseHolder<V extends ViewBinding> extends RecyclerView.ViewHolder {
- private V viewBinding;
-
- public BaseHolder(V viewBinding) {
- super(viewBinding.getRoot());
- this.viewBinding = viewBinding;
- }
-
- public V getViewBinding() {
- return viewBinding;
- }
- }
之后在使用Adapter的时候,将item的ViewBinding填好后,可以直接操控item的控件,赋值,或者加载图片,所有简单的recycleview都可以直接通用,设计非常的银杏,十分好用
- BaseAdapter<String, ItemDeviceBinding> adapter = new BaseAdapter<String, ItemDeviceBinding>(aa) {
- @Override
- protected void onBindingData(BaseHolder<ItemDeviceBinding> holder, String s, int position) {
- holder.getViewBinding().tvName.setText("123");
-
- }
-
- @Override
- protected ItemDeviceBinding onBindingView(ViewGroup viewGroup) {
- ItemDeviceBinding viewBinding = ItemDeviceBinding.inflate(LayoutInflater.from(viewGroup.getContext()), viewGroup, false);
- return viewBinding;
- }
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。