/
RecyclerViewBackedScrollView.java
469 lines (400 loc) · 18.5 KB
/
RecyclerViewBackedScrollView.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package com.github.godness84.RNRecyclerViewList;
import android.content.Context;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.common.SystemClock;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.NativeGestureUtil;
import com.facebook.react.views.scroll.OnScrollDispatchHelper;
import com.facebook.react.views.scroll.ScrollEvent;
import com.facebook.react.views.scroll.ScrollEventType;
import com.facebook.react.views.scroll.VelocityHelper;
import java.util.ArrayList;
import java.util.List;
/**
* Wraps {@link RecyclerView} providing interface similar to `ScrollView.js` where each children
* will be rendered as a separate {@link RecyclerView} row.
* <p>
* Currently supports only vertically positioned item. Views will not be automatically recycled but
* they will be detached from native view hierarchy when scrolled offscreen.
* <p>
* It works by storing all child views in an array within adapter and binding appropriate views to
* rows when requested.
*/
@VisibleForTesting
public class RecyclerViewBackedScrollView extends RecyclerView {
private final static String TAG = "RecyclerViewBackedScrol";
private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper();
private final VelocityHelper mVelocityHelper = new VelocityHelper();
static class ScrollOptions {
@Nullable Float millisecondsPerInch;
@Nullable Float viewPosition;
@Nullable Float viewOffset;
}
/**
* Simple implementation of {@link ViewHolder} as it's an abstract class. The only thing we need
* to hold in this implementation is the reference to {@link RecyclableWrapperViewGroup} that
* is already stored by default.
*/
private static class ConcreteViewHolder extends ViewHolder {
public ConcreteViewHolder(View itemView) {
super(itemView);
}
}
/**
* View that is going to be used as a cell in {@link RecyclerView}. It's going to be reusable and
* we will remove/attach views for a certain positions based on the {@code mViews} array stored
* in the adapter class.
* <p>
* This method overrides {@link #onMeasure} and delegates measurements to the child view that has
* been attached to. This is because instances of {@link RecyclableWrapperViewGroup} are created
* outside of {@link } and their layout is not managed by that manager
* as opposed to all the other react-native views. Instead we use dimensions of the child view
* (dimensions has been set in layouting process) so that size of this view match the size of
* the view it wraps.
*/
static class RecyclableWrapperViewGroup extends ViewGroup {
private ReactListAdapter mAdapter;
private int mLastMeasuredWidth;
private int mLastMeasuredHeight;
public RecyclableWrapperViewGroup(Context context, ReactListAdapter adapter) {
super(context);
mAdapter = adapter;
mLastMeasuredHeight = 10;
mLastMeasuredWidth = 10;
}
private OnLayoutChangeListener mChildLayoutChangeListener = new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
int oldHeight = (oldBottom - oldTop);
int newHeight = (bottom - top);
if (oldHeight != newHeight) {
if (getParent() != null) {
requestLayout();
getParent().requestLayout();
}
}
}
};
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// This view will only have one child that is managed by the `NativeViewHierarchyManager` and
// its position and dimensions are set separately. We don't need to handle its layouting here
}
@Override
public void onViewAdded(View child) {
super.onViewAdded(child);
child.addOnLayoutChangeListener(mChildLayoutChangeListener);
}
@Override
public void onViewRemoved(View child) {
super.onViewRemoved(child);
child.removeOnLayoutChangeListener(mChildLayoutChangeListener);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// We override measure spec and use dimensions of the children. Children is a view added
// from the adapter and always have a correct dimensions specified as they are calculated
// and set with NativeViewHierarchyManager.
// In case there is no view attached, we use the last measured dimensions.
if (getChildCount() > 0) {
View child = getChildAt(0);
mLastMeasuredWidth = child.getMeasuredWidth();
mLastMeasuredHeight = child.getMeasuredHeight();
setMeasuredDimension(mLastMeasuredWidth, mLastMeasuredHeight);
} else {
setMeasuredDimension(mLastMeasuredWidth, mLastMeasuredHeight);
}
}
public ReactListAdapter getAdapter() {
return mAdapter;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Similarly to ReactViewGroup, we return true.
// In this case it is necessary in order to force the RecyclerView to intercept the touch events,
// in this way we can exactly know when the drag starts because "onInterceptTouchEvent"
// of the RecyclerView will return true.
return true;
}
}
/*package*/ static class ReactListAdapter extends Adapter<ConcreteViewHolder> {
private final List<RecyclerViewItemView> mViews = new ArrayList<>();
private final RecyclerViewBackedScrollView mScrollView;
private int mItemCount = 0;
public ReactListAdapter(RecyclerViewBackedScrollView scrollView) {
mScrollView = scrollView;
//setHasStableIds(true);
}
public void addView(RecyclerViewItemView child, int index) {
mViews.add(index, child);
final int itemIndex = child.getItemIndex();
notifyItemChanged(itemIndex);
}
public void removeViewAt(int index) {
RecyclerViewItemView child = mViews.get(index);
if (child != null) {
mViews.remove(index);
}
}
public int getViewCount() {
return mViews.size();
}
@Override
public ConcreteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ConcreteViewHolder(new RecyclableWrapperViewGroup(parent.getContext(), this));
}
@Override
public void onBindViewHolder(ConcreteViewHolder holder, int position) {
RecyclableWrapperViewGroup vg = (RecyclableWrapperViewGroup) holder.itemView;
View row = getViewByItemIndex(position);
if (row != null && row.getParent() != vg) {
if (row.getParent() != null) {
((ViewGroup) row.getParent()).removeView(row);
}
vg.addView(row, 0);
}
}
@Override
public void onViewRecycled(ConcreteViewHolder holder) {
super.onViewRecycled(holder);
((RecyclableWrapperViewGroup) holder.itemView).removeAllViews();
}
@Override
public int getItemCount() {
return mItemCount;
}
public void setItemCount(int itemCount) {
this.mItemCount = itemCount;
}
public View getView(int index) {
return mViews.get(index);
}
public RecyclerViewItemView getViewByItemIndex(int position) {
for (int i = 0; i < mViews.size(); i++) {
if (mViews.get(i).getItemIndex() == position) {
return mViews.get(i);
}
}
return null;
}
}
private boolean mDragging;
private int mFirstVisibleIndex, mLastVisibleIndex;
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollDispatchHelper.onScrollChanged(l, t)) {
getReactContext().getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(ScrollEvent.obtain(
getId(),
ScrollEventType.SCROLL,
0, /* offsetX = 0, horizontal scrolling only */
computeVerticalScrollOffset(),
mOnScrollDispatchHelper.getXFlingVelocity(),
mOnScrollDispatchHelper.getYFlingVelocity(),
getWidth(),
computeVerticalScrollRange(),
getWidth(),
getHeight()));
}
final int firstIndex = ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition();
final int lastIndex = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
if (firstIndex != mFirstVisibleIndex || lastIndex != mLastVisibleIndex) {
getReactContext().getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(new VisibleItemsChangeEvent(
getId(),
SystemClock.nanoTime(),
firstIndex,
lastIndex));
mFirstVisibleIndex = firstIndex;
mLastVisibleIndex = lastIndex;
}
}
private ReactContext getReactContext() {
return (ReactContext) ((ContextThemeWrapper) getContext()).getBaseContext();
}
public RecyclerViewBackedScrollView(Context context) {
super(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));
setHasFixedSize(true);
((DefaultItemAnimator)getItemAnimator()).setSupportsChangeAnimations(false);
setLayoutManager(new LinearLayoutManager(context));
setAdapter(new ReactListAdapter(this));
}
/*package*/ void addViewToAdapter(RecyclerViewItemView child, int index) {
((ReactListAdapter) getAdapter()).addView(child, index);
}
/*package*/ void removeViewFromAdapter(int index) {
((ReactListAdapter) getAdapter()).removeViewAt(index);
}
/*package*/ View getChildAtFromAdapter(int index) {
return ((ReactListAdapter) getAdapter()).getView(index);
}
/*package*/ int getChildCountFromAdapter() {
return ((ReactListAdapter) getAdapter()).getViewCount();
}
/*package*/ void setItemCount(int itemCount) {
((ReactListAdapter) getAdapter()).setItemCount(itemCount);
}
/*package*/ int getItemCount() {
return getAdapter().getItemCount();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (super.onInterceptTouchEvent(ev)) {
NativeGestureUtil.notifyNativeGestureStarted(this, ev);
mDragging = true;
getReactContext().getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(ScrollEvent.obtain(
getId(),
ScrollEventType.BEGIN_DRAG,
0, /* offsetX = 0, horizontal scrolling only */
computeVerticalScrollOffset(),
0, // xVelocity
0, // yVelocity
getWidth(),
computeVerticalScrollRange(),
getWidth(),
getHeight()));
return true;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_UP && mDragging) {
mDragging = false;
mVelocityHelper.calculateVelocity(ev);
getReactContext().getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(ScrollEvent.obtain(
getId(),
ScrollEventType.END_DRAG,
0, /* offsetX = 0, horizontal scrolling only */
computeVerticalScrollOffset(),
mVelocityHelper.getXVelocity(),
mVelocityHelper.getYVelocity(),
getWidth(),
computeVerticalScrollRange(),
getWidth(),
getHeight()));
}
return super.onTouchEvent(ev);
}
private boolean mRequestedLayout = false;
@Override
public void requestLayout() {
super.requestLayout();
if (!mRequestedLayout) {
mRequestedLayout = true;
this.post(new Runnable() {
@Override
public void run() {
mRequestedLayout = false;
layout(getLeft(), getTop(), getRight(), getBottom());
onLayout(false, getLeft(), getTop(), getRight(), getBottom());
}
});
}
}
@Override
public void scrollToPosition(int position) {
this.scrollToPosition(position, new ScrollOptions());
}
public void scrollToPosition(final int position, final ScrollOptions options) {
if (options.viewPosition != null) {
final LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
final ReactListAdapter adapter = (ReactListAdapter) getAdapter();
final View view = adapter.getViewByItemIndex(position);
if (view != null) {
final int viewHeight = view.getHeight();
// In order to calculate the correct offset, we need the height of the target view.
// If the height of the view is not available it means RN has not calculated it yet.
// So let's listen to the layout change and we will retry scrolling.
if (viewHeight == 0) {
view.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
scrollToPosition(position, options);
}
});
return;
}
final int boxStart = layoutManager.getPaddingTop();
final int boxEnd = layoutManager.getHeight() - layoutManager.getPaddingBottom();
final int boxHeight = boxEnd - boxStart;
float viewOffset = options.viewOffset != null ? PixelUtil.toPixelFromDIP(options.viewOffset) : 0;
int offset = (int) ((boxHeight - viewHeight) * options.viewPosition + viewOffset);
layoutManager.scrollToPositionWithOffset(position, offset);
return;
}
}
super.scrollToPosition(position);
}
@Override
public void smoothScrollToPosition(int position) {
this.smoothScrollToPosition(position, new ScrollOptions());
}
public void smoothScrollToPosition(int position, final ScrollOptions options) {
final RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(this.getContext()) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return ((LinearLayoutManager) this.getLayoutManager()).computeScrollVectorForPosition(targetPosition);
}
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
if (options.millisecondsPerInch != null) {
return options.millisecondsPerInch / displayMetrics.densityDpi;
} else {
return super.calculateSpeedPerPixel(displayMetrics);
}
}
@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
int calc = super.calculateDtToFit(viewStart, viewEnd, boxStart, boxEnd, snapPreference);
if (options.viewPosition != null) {
int viewHeight = viewEnd - viewStart;
int boxHeight = boxEnd - boxStart;
float viewOffset = options.viewOffset != null ? PixelUtil.toPixelFromDIP(options.viewOffset) : 0;
float target = boxStart + (boxHeight - viewHeight) * options.viewPosition + viewOffset;
return (int) (target - viewStart);
} else {
return super.calculateDtToFit(viewStart, viewEnd, boxStart, boxEnd, snapPreference);
}
}
};
smoothScroller.setTargetPosition(position);
this.getLayoutManager().startSmoothScroll(smoothScroller);
}
public void setInverted(boolean inverted) {
LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
layoutManager.setReverseLayout(inverted);
}
public void setItemAnimatorEnabled(boolean enabled) {
if (enabled) {
DefaultItemAnimator animator = new DefaultItemAnimator();
animator.setSupportsChangeAnimations(false);
setItemAnimator(animator);
} else {
setItemAnimator(null);
}
}
}