Runt
2022-08-13 b3a51f064c4dfb27f54cd9526803338d2e8dc296
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package com.runt.open.mvvm.base.adapter;
 
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;
import com.runt.open.mvvm.databinding.LayoutNullBinding;
import com.runt.open.mvvm.util.DimensionUtils;
 
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by Administrator on 2021/10/27 0027.
 *  DATA  数据类型
 *  VB 适配器视图
 */
public abstract class BaseAdapter<DATA, VB extends ViewBinding> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
 
    protected List<DATA> dataList = new ArrayList<>();
    protected OnItemClickListener<DATA> onItemClickListener;
    public boolean showNull;
    public float defaultMarginBottom,lastMarginBottom;
 
    public interface  OnItemClickListener<DATA>{
        void onItemClick(int position,DATA data);
    }
 
    public class ViewBindHolder extends RecyclerView.ViewHolder  {
        ViewBinding binding;
        public ViewBindHolder(ViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
 
    public void setOnItemClickListener(OnItemClickListener<DATA> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
 
    public void setData(List<DATA> list){
        if(dataList != list) {
            dataList.clear();
            if (list != null) {
                dataList.addAll(list);
            }
        }
        notifyDataSetChanged();
    }
 
    public void addData(DATA data){
        if(data != null){
            dataList.add(data);
        }
        notifyDataSetChanged();
    }
 
    public void addData(List<DATA> list){
        if (list != null && list.size() > 0) {
            this.dataList.addAll(list);
        }
        notifyDataSetChanged();
    }
 
    public List<DATA> getData() {
        return dataList;
    }
 
    @NonNull
    @Override
    public ViewBindHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType == 1 ){
            try {
                //实例化viewbind
                Class<VB> entityClass = (Class<VB>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
                Method method = entityClass.getMethod("inflate", LayoutInflater.class,ViewGroup.class,boolean.class);//get method from name "inflate";
                VB vBind = (VB) method.invoke(entityClass,LayoutInflater.from(parent.getContext()),parent,false);//execute method to create a objct of viewbind;
                return new ViewBindHolder(vBind);
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                Throwable t = e.getTargetException();// 获取目标异常
                t.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //加载空数据
        return new ViewBindHolder( LayoutNullBinding.inflate( LayoutInflater.from(parent.getContext()), parent, false ) );
    }
 
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewBindHolder bindHolder = (ViewBindHolder) holder;
        if(getItemViewType(position) == 0 || bindHolder.binding instanceof LayoutNullBinding){
            onBindEmptyView((LayoutNullBinding) bindHolder.binding);
        }else{
            if (onItemClickListener != null) {
                bindHolder.binding.getRoot().setOnClickListener(view -> {
                    if(onItemClickListener != null){
                        onItemClickListener.onItemClick(position,getItem(position));
                    }
                });
            }
            onBindView((VB) bindHolder.binding, position, getItem(position));
            setBottomMargin(bindHolder,position);
        }
    }
 
    /**
     * 设置最后一个底部间隔
     * @param holder
     * @param position
     */
    protected void setBottomMargin(ViewBindHolder holder, int position){
        setBottomMargin(holder,position,lastMarginBottom);
    }
 
    /**
     * 设置最后一个底部间隔
     * @param holder
     * @param position  位置
     * @param dp        间距
     */
    protected void setBottomMargin(RecyclerView.ViewHolder holder, int position,float dp){
        setBottomMargin(holder,position,dp,defaultMarginBottom);
    }
 
    protected void setBottomMargin(RecyclerView.ViewHolder holder, int position, float dp, float defaultDp){
        ViewGroup.MarginLayoutParams params1 = (ViewGroup.MarginLayoutParams) holder.itemView.getLayoutParams();
        if(position == dataList.size() -1){
            params1.setMargins(params1.leftMargin, params1.topMargin, params1.rightMargin, (int) DimensionUtils.convertDpToPixel(dp,holder.itemView.getContext()));
        }else{
            params1.setMargins(params1.leftMargin, params1.topMargin, params1.rightMargin, (int) DimensionUtils.convertDpToPixel(defaultDp,holder.itemView.getContext()));
        }
    }
 
 
    /**
     * 空数据支持
     * @return
     */
    @Override
    public int getItemCount() {
        return (showNull && dataList.size() == 0 )? 1 : dataList.size();
    }
 
    /**
     * 当下标为0,数据集合为0 返回0(意味当前应显示空数据视图))
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return  (showNull && position == 0 && dataList.size() == 0)? 0 : 1;
    }
 
    public DATA getItem(int position){
        if(position >= dataList.size()){
            return null;
        }else {
            return dataList.get(position);
        }
    }
 
    protected abstract void onBindView(VB binding, int position, DATA data);
 
    /**
     * 空数据显示
     */
    protected void onBindEmptyView(LayoutNullBinding emptyBinding){
        Log.e("baseAdapter"," emptyBinding:"+emptyBinding);
 
    }
}