Skip to content

Commit 71ec85f

Browse files
mdvaccafacebook-github-bot
authored andcommittedJan 18, 2018
Cleanup ReactHorizontalScrollView
Reviewed By: achen1 Differential Revision: D6693605 fbshipit-source-id: e58425b2a5b0bf75ffb41ef3ab6fa7ad46222531
1 parent 52ffa5d commit 71ec85f

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed
 

‎ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.graphics.Rect;
1717
import android.graphics.drawable.ColorDrawable;
1818
import android.graphics.drawable.Drawable;
19+
import android.support.v4.view.ViewCompat;
1920
import android.view.MotionEvent;
2021
import android.view.View;
2122
import android.widget.HorizontalScrollView;
@@ -30,6 +31,7 @@
3031
/**
3132
* Similar to {@link ReactScrollView} but only supports horizontal scrolling.
3233
*/
34+
@TargetApi(16)
3335
public class ReactHorizontalScrollView extends HorizontalScrollView implements
3436
ReactClippingViewGroup {
3537

@@ -271,7 +273,6 @@ public void draw(Canvas canvas) {
271273
* don't get any events from Android about this lifecycle, we do all our detection by creating a
272274
* runnable that checks if we scrolled in the last frame and if so assumes we are still scrolling.
273275
*/
274-
@TargetApi(16)
275276
private void handlePostTouchScrolling(int velocityX, int velocityY) {
276277
// If we aren't going to do anything (send events or snap to page), we can early out.
277278
if (!mSendMomentumEvents && !mPagingEnabled && !isScrollPerfLoggingEnabled()) {
@@ -298,14 +299,18 @@ public void run() {
298299
if (mActivelyScrolling) {
299300
// We are still scrolling so we just post to check again a frame later
300301
mActivelyScrolling = false;
301-
ReactHorizontalScrollView.this.postOnAnimationDelayed(this, ReactScrollViewHelper.MOMENTUM_DELAY);
302+
ViewCompat.postOnAnimationDelayed(ReactHorizontalScrollView.this,
303+
this,
304+
ReactScrollViewHelper.MOMENTUM_DELAY);
302305
} else {
303306
if (mPagingEnabled && !mSnappingToPage) {
304307
// Only if we have pagingEnabled and we have not snapped to the page do we
305308
// need to continue checking for the scroll. And we cause that scroll by asking for it
306309
mSnappingToPage = true;
307310
smoothScrollToPage(0);
308-
ReactHorizontalScrollView.this.postOnAnimationDelayed(this, ReactScrollViewHelper.MOMENTUM_DELAY);
311+
ViewCompat.postOnAnimationDelayed(ReactHorizontalScrollView.this,
312+
this,
313+
ReactScrollViewHelper.MOMENTUM_DELAY);
309314
} else {
310315
if (mSendMomentumEvents) {
311316
ReactScrollViewHelper.emitScrollMomentumEndEvent(ReactHorizontalScrollView.this);
@@ -316,7 +321,9 @@ public void run() {
316321
}
317322
}
318323
};
319-
postOnAnimationDelayed(mPostTouchRunnable, ReactScrollViewHelper.MOMENTUM_DELAY);
324+
ViewCompat.postOnAnimationDelayed(ReactHorizontalScrollView.this,
325+
mPostTouchRunnable,
326+
ReactScrollViewHelper.MOMENTUM_DELAY);
320327
}
321328

322329
/**

‎ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package com.facebook.react.views.scroll;
1111

12+
import android.annotation.TargetApi;
1213
import android.graphics.Canvas;
1314
import android.graphics.Color;
1415
import android.graphics.Rect;
@@ -39,13 +40,14 @@
3940
* <p>ReactScrollView only supports vertical scrolling. For horizontal scrolling,
4041
* use {@link ReactHorizontalScrollView}.
4142
*/
43+
@TargetApi(11)
4244
public class ReactScrollView extends ScrollView implements ReactClippingViewGroup, ViewGroup.OnHierarchyChangeListener, View.OnLayoutChangeListener {
4345

44-
private static Field sScrollerField;
46+
private static @Nullable Field sScrollerField;
4547
private static boolean sTriedToGetScrollerField = false;
4648

4749
private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper();
48-
private final OverScroller mScroller;
50+
private final @Nullable OverScroller mScroller;
4951
private final VelocityHelper mVelocityHelper = new VelocityHelper();
5052

5153
private @Nullable Rect mClippingRect;
@@ -71,6 +73,15 @@ public ReactScrollView(ReactContext context, @Nullable FpsListener fpsListener)
7173
mFpsListener = fpsListener;
7274
mReactBackgroundManager = new ReactViewBackgroundManager(this);
7375

76+
mScroller = getOverScrollerFromParent();
77+
setOnHierarchyChangeListener(this);
78+
setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
79+
}
80+
81+
@Nullable
82+
private OverScroller getOverScrollerFromParent() {
83+
OverScroller scroller;
84+
7485
if (!sTriedToGetScrollerField) {
7586
sTriedToGetScrollerField = true;
7687
try {
@@ -86,32 +97,31 @@ public ReactScrollView(ReactContext context, @Nullable FpsListener fpsListener)
8697

8798
if (sScrollerField != null) {
8899
try {
89-
Object scroller = sScrollerField.get(this);
90-
if (scroller instanceof OverScroller) {
91-
mScroller = (OverScroller) scroller;
100+
Object scrollerValue = sScrollerField.get(this);
101+
if (scrollerValue instanceof OverScroller) {
102+
scroller = (OverScroller) scrollerValue;
92103
} else {
93104
Log.w(
94105
ReactConstants.TAG,
95106
"Failed to cast mScroller field in ScrollView (probably due to OEM changes to AOSP)! " +
96107
"This app will exhibit the bounce-back scrolling bug :(");
97-
mScroller = null;
108+
scroller = null;
98109
}
99110
} catch (IllegalAccessException e) {
100111
throw new RuntimeException("Failed to get mScroller from ScrollView!", e);
101112
}
102113
} else {
103-
mScroller = null;
114+
scroller = null;
104115
}
105116

106-
setOnHierarchyChangeListener(this);
107-
setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
117+
return scroller;
108118
}
109119

110120
public void setSendMomentumEvents(boolean sendMomentumEvents) {
111121
mSendMomentumEvents = sendMomentumEvents;
112122
}
113123

114-
public void setScrollPerfTag(String scrollPerfTag) {
124+
public void setScrollPerfTag(@Nullable String scrollPerfTag) {
115125
mScrollPerfTag = scrollPerfTag;
116126
}
117127

‎ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package com.facebook.react.views.scroll;
1111

12+
import android.annotation.TargetApi;
1213
import android.graphics.Color;
1314
import com.facebook.react.bridge.ReadableArray;
1415
import com.facebook.react.common.MapBuilder;
@@ -31,6 +32,7 @@
3132
* <p>Note that {@link ReactScrollView} and {@link ReactHorizontalScrollView} are exposed to JS
3233
* as a single ScrollView component, configured via the {@code horizontal} boolean property.
3334
*/
35+
@TargetApi(11)
3436
@ReactModule(name = ReactScrollViewManager.REACT_CLASS)
3537
public class ReactScrollViewManager
3638
extends ViewGroupManager<ReactScrollView>
@@ -98,7 +100,7 @@ public void setSendMomentumEvents(ReactScrollView view, boolean sendMomentumEven
98100
* @param scrollPerfTag
99101
*/
100102
@ReactProp(name = "scrollPerfTag")
101-
public void setScrollPerfTag(ReactScrollView view, String scrollPerfTag) {
103+
public void setScrollPerfTag(ReactScrollView view, @Nullable String scrollPerfTag) {
102104
view.setScrollPerfTag(scrollPerfTag);
103105
}
104106

@@ -191,8 +193,8 @@ public void setBorderWidth(ReactScrollView view, int index, float width) {
191193
}, customType = "Color")
192194
public void setBorderColor(ReactScrollView view, int index, Integer color) {
193195
float rgbComponent =
194-
color == null ? YogaConstants.UNDEFINED : (float) ((int)color & 0x00FFFFFF);
195-
float alphaComponent = color == null ? YogaConstants.UNDEFINED : (float) ((int)color >>> 24);
196+
color == null ? YogaConstants.UNDEFINED : (float) (color & 0x00FFFFFF);
197+
float alphaComponent = color == null ? YogaConstants.UNDEFINED : (float) (color >>> 24);
196198
view.setBorderColor(SPACING_TYPES[index], rgbComponent, alphaComponent);
197199
}
198200

@@ -211,12 +213,12 @@ public void scrollToEnd(
211213
}
212214

213215
@Override
214-
public @Nullable Map getExportedCustomDirectEventTypeConstants() {
216+
public @Nullable Map<String, Object> getExportedCustomDirectEventTypeConstants() {
215217
return createExportedCustomDirectEventTypeConstants();
216218
}
217219

218-
public static Map createExportedCustomDirectEventTypeConstants() {
219-
return MapBuilder.builder()
220+
public static Map<String, Object> createExportedCustomDirectEventTypeConstants() {
221+
return MapBuilder.<String, Object>builder()
220222
.put(ScrollEventType.SCROLL.getJSEventName(), MapBuilder.of("registrationName", "onScroll"))
221223
.put(ScrollEventType.BEGIN_DRAG.getJSEventName(), MapBuilder.of("registrationName", "onScrollBeginDrag"))
222224
.put(ScrollEventType.END_DRAG.getJSEventName(), MapBuilder.of("registrationName", "onScrollEndDrag"))

0 commit comments

Comments
 (0)
Please sign in to comment.