BaseAdapter.java
4.99 KB
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
package com.ys.yslibrary.ui.adapter;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder> implements View.OnClickListener {
private Activity context;
private List<T> list;
private LayoutInflater inflate;
public BaseAdapter(Activity context, List<T> list) {
if (context == null) {
throw new NullPointerException("context can't NULL");
}
this.context = context;
this.list = list;
this.inflate = LayoutInflater.from(context);
}
public T getItemData(int pos) {
return list.get(pos);
}
public Activity getContext() {
return context;
}
public List<T> getList() {
return list;
}
@NonNull
@Override
public final BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new BaseViewHolder(DataBindingUtil.inflate(inflate, getItemLayoutId(viewType), parent, false));
}
@Override
public final void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
bindHolder(holder.binding, position, getItemData(position), getItemViewType(position));
}
@Override
public final void onBindViewHolder(@NonNull BaseViewHolder holder, int position, @NonNull List<Object> payloads) {
super.onBindViewHolder(holder, position, payloads);
bindHolder(holder.binding, position, getItemData(position), payloads);
}
@Override
public int getItemCount() {
return list.size();
}
abstract int getItemLayoutId(int viewType);
abstract void bindHolder(ViewDataBinding viewDataBinding, int position, T itemBean, int viewType);
public void bindHolder(ViewDataBinding viewDataBinding, int position, T itemBean, @NonNull List<Object> payloads) {
}
@Override
public void onClick(View v) {
}
public static class BaseViewHolder<B extends ViewDataBinding> extends RecyclerView.ViewHolder {
private final B binding;
public BaseViewHolder(B binding) {
super(binding.getRoot());
this.binding = binding;
}
}
public void clear() {
if (list != null) {
list.clear();
}
notifyDataSetChanged();
}
/**
* 是否是相同的item
*
* @param oldItem
* @param newItem
* @return
*/
public abstract boolean areItemsTheSame(T oldItem, T newItem);
public void replaceData(List<T> newData) {
if (newData == null || newData.isEmpty()) {
return;
}
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffCallBack(list, newData), true);
result.dispatchUpdatesTo(this);
this.list = newData;
}
public void insertData(List<T> newData) {
if (list == null || newData == null || newData.isEmpty()) {
return;
}
int index = list.size();
if (!list.containsAll(newData)) {
list.addAll(newData);
}
if (index != list.size()) {
notifyItemRangeInserted(index, list.size());
}
}
/**
* 是否内容相同
*
* @param oldItem
* @param newItem
* @return
*/
public abstract boolean areContentsTheSame(T oldItem, T newItem);
private class DiffCallBack extends DiffUtil.Callback {
private List<T> oldData;
private List<T> newData;
public DiffCallBack(List<T> oldData, List<T> newData) {
this.oldData = oldData;
this.newData = newData;
}
@Override
public int getOldListSize() {
return oldData != null ? oldData.size() : 0;
}
@Override
public int getNewListSize() {
return newData != null ? newData.size() : 0;
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
T oldT = oldData.get(oldItemPosition);
T newT = oldData.get(newItemPosition);
// 若此处返回true,则DiffUtil会再调用下面的areContentsTheSame方法,进一步对比UI是否有变化
// 若此处返回false,则说明id都不同,肯定不是一个item
return BaseAdapter.this.areItemsTheSame(oldT, newT);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
// TODO 比较新旧数据(主要是UI展示内容)是否相同,这里为了方便示例直接返回true
T oldT = oldData.get(oldItemPosition);
T newT = oldData.get(newItemPosition);
return BaseAdapter.this.areItemsTheSame(oldT, newT);
}
}
}