-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRecyclerViewDatabindingAdapter.java
263 lines (222 loc) · 8.49 KB
/
RecyclerViewDatabindingAdapter.java
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Copyright (C) 2018, Brian He
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.brianspace.databinding.adapter;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* {@link android.support.v7.widget.RecyclerView} adapter supporting data binding to
* {@link android.databinding.ObservableList}.
*
* @param <ItemTypeT> type of the items in the {@link android.databinding.ObservableList}.
*/
public class RecyclerViewDatabindingAdapter<ItemTypeT>
extends RecyclerView.Adapter<RecyclerViewDatabindingAdapter<ItemTypeT>.BindingHolder> {
// region Private Fields
/**
* The list of items for binding.
*/
private final ObservableList<ItemTypeT> itemList;
/**
* The internal list of items for the adapter, to avoid the crash caused by inconsistency.
*/
@SuppressWarnings("WeakerAccess")
protected final List<ItemTypeT> adapterItems = new ArrayList<>();
/**
* The layout ID used for inflating the item views.
*/
@LayoutRes
private final int itemLayoutId;
/**
* Data binding ID (BR.*) for the <data> element used for binding in the view's layout xml.
*/
private final int itemDataBrId;
/**
* The callback for the list changed events. Use Handler of the main looper to avoid the exception:
* IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
*/
private final ObservableList.OnListChangedCallback<ObservableList<ItemTypeT>> listChangedCallback =
new ObservableList.OnListChangedCallback<ObservableList<ItemTypeT>>() {
/**
* Handler of the main looper.
*/
private final Handler mainHandler = new Handler(Looper.getMainLooper());
@Override
public void onChanged(final ObservableList<ItemTypeT> items) {
onItemChanged(items);
}
@Override
public void onItemRangeChanged(final ObservableList<ItemTypeT> items,
final int start, final int count) {
mainHandler.post(() -> {
for (int i = start; i < start + count; ++i) {
adapterItems.set(i, items.get(i));
}
notifyItemRangeChanged(getItemLayoutPosition(start), count);
});
}
@Override
public void onItemRangeInserted(final ObservableList<ItemTypeT> items,
final int start, final int count) {
mainHandler.post(() -> {
for (int i = start + count - 1; i >= start; --i) {
adapterItems.add(start, items.get(i));
}
notifyItemRangeInserted(getItemLayoutPosition(start), count);
});
}
@Override
public void onItemRangeMoved(final ObservableList<ItemTypeT> items,
final int start, final int to, final int count) {
onItemChanged(items);
}
@Override
public void onItemRangeRemoved(final ObservableList<ItemTypeT> items,
final int start, final int count) {
mainHandler.post(() -> {
for (int i = 0; i < count; ++i) {
adapterItems.remove(start);
}
notifyItemRangeRemoved(getItemLayoutPosition(start), count);
});
}
private void onItemChanged(final ObservableList<ItemTypeT> items) {
mainHandler.post(() -> {
adapterItems.clear();
adapterItems.addAll(items);
notifyDataSetChanged();
});
}
};
// endregion
// region Public Inner Types
/**
* View holder for the items.
*/
public class BindingHolder extends RecyclerView.ViewHolder {
/**
* Binding for the view.
*/
private ViewDataBinding binding;
/**
* Constructor.
*
* @param itemView view for the list item.
*/
public BindingHolder(@NonNull final View itemView) {
super(itemView);
}
/**
* Get the binding.
*/
public ViewDataBinding getBinding() {
return binding;
}
/**
* Set the binding.
*/
public void setBinding(final ViewDataBinding binding) {
this.binding = binding;
}
}
// endregion
// region Constructors
/**
* Constructor for RecyclerViewDatabindingAdapter.
*
* @param itemList Observable item list.
* @param itemDataBrId Variable name in <data> element used for binding in the item layout.
* @param itemLayoutId Layout ID for list item.
*/
public RecyclerViewDatabindingAdapter(@NonNull final ObservableList<ItemTypeT> itemList, final int itemDataBrId,
@LayoutRes final int itemLayoutId) {
this.itemList = itemList;
this.itemDataBrId = itemDataBrId;
this.itemLayoutId = itemLayoutId;
}
// endregion
// region Public Overrides
@Override
public BindingHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
final ViewDataBinding itemBinding =
DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), itemLayoutId, parent, false);
final BindingHolder holder = new BindingHolder(itemBinding.getRoot());
holder.setBinding(itemBinding);
return holder;
}
@Override
public void onBindViewHolder(final RecyclerViewDatabindingAdapter<ItemTypeT>.BindingHolder holder,
final int position) {
final Object item = getItemForBinding(position);
final ViewDataBinding itemBinding = holder.getBinding();
itemBinding.setVariable(itemDataBrId, item);
itemBinding.executePendingBindings();
}
@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
if (!itemList.isEmpty()) {
listChangedCallback.onChanged(itemList);
}
this.itemList.addOnListChangedCallback(listChangedCallback);
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public void onDetachedFromRecyclerView(final RecyclerView recyclerView) {
this.itemList.removeOnListChangedCallback(listChangedCallback);
super.onDetachedFromRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return adapterItems.size();
}
// endregion
// region Protected Methods
/**
* Get the item for binding.
* Headered list should override this.
*
* @param position item position.
* @return the item object at the position.
*/
@SuppressWarnings("WeakerAccess")
@Nullable
protected Object getItemForBinding(final int position) {
return adapterItems.get(position);
}
/**
* Get the layout position of the item.
* Headered list should override this.
*
* @param position the position of item in the item list.
* @return the corresponding layout position in the recycler view.
*/
@SuppressWarnings("WeakerAccess")
protected int getItemLayoutPosition(final int position) {
return position;
}
// endregion
}