Recycle scroll events.

Reviewed By: mkonicek

Differential Revision: D2591945

fb-gh-sync-id: 1c9bfdc88ef5add38fcf0c9499bf52a8a940a22e
This commit is contained in:
Krzysztof Magiera 2015-10-28 14:24:18 -07:00 committed by facebook-github-bot-3
parent 97f1cdaad4
commit 03d7c7a6a1
3 changed files with 58 additions and 19 deletions

View File

@ -247,17 +247,16 @@ public class RecyclerViewBackedScrollView extends RecyclerView {
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
ScrollEvent event = new ScrollEvent(
getId(),
SystemClock.uptimeMillis(),
0, /* offsetX = 0, horizontal scrolling only */
calculateAbsoluteOffset(),
getWidth(),
((ReactListAdapter) getAdapter()).getTotalChildrenHeight(),
getWidth(),
getHeight());
((ReactContext) getContext()).getNativeModule(UIManagerModule.class).getEventDispatcher()
.dispatchEvent(event);
.dispatchEvent(ScrollEvent.obtain(
getId(),
SystemClock.uptimeMillis(),
0, /* offsetX = 0, horizontal scrolling only */
calculateAbsoluteOffset(),
getWidth(),
((ReactListAdapter) getAdapter()).getTotalChildrenHeight(),
getWidth(),
getHeight()));
}
public RecyclerViewBackedScrollView(Context context) {

View File

@ -28,7 +28,7 @@ public class ReactScrollViewHelper {
View contentView = scrollView.getChildAt(0);
ReactContext reactContext = (ReactContext) scrollView.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(
new ScrollEvent(
ScrollEvent.obtain(
scrollView.getId(),
SystemClock.uptimeMillis(),
scrollX,

View File

@ -9,6 +9,10 @@
package com.facebook.react.views.scroll;
import java.lang.Override;
import android.support.v4.util.Pools;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.PixelUtil;
@ -20,16 +24,19 @@ import com.facebook.react.uimanager.events.RCTEventEmitter;
*/
public class ScrollEvent extends Event<ScrollEvent> {
private static final Pools.SynchronizedPool<ScrollEvent> EVENTS_POOL =
new Pools.SynchronizedPool<>(3);
public static final String EVENT_NAME = "topScroll";
private final int mScrollX;
private final int mScrollY;
private final int mContentWidth;
private final int mContentHeight;
private final int mScrollViewWidth;
private final int mScrollViewHeight;
private int mScrollX;
private int mScrollY;
private int mContentWidth;
private int mContentHeight;
private int mScrollViewWidth;
private int mScrollViewHeight;
public ScrollEvent(
public static ScrollEvent obtain(
int viewTag,
long timestampMs,
int scrollX,
@ -38,7 +45,40 @@ public class ScrollEvent extends Event<ScrollEvent> {
int contentHeight,
int scrollViewWidth,
int scrollViewHeight) {
super(viewTag, timestampMs);
ScrollEvent event = EVENTS_POOL.acquire();
if (event == null) {
event = new ScrollEvent();
}
event.init(
viewTag,
timestampMs,
scrollX,
scrollY,
contentWidth,
contentHeight,
scrollViewWidth,
scrollViewHeight);
return event;
}
@Override
public void onDispose() {
EVENTS_POOL.release(this);
}
private ScrollEvent() {
}
private void init(
int viewTag,
long timestampMs,
int scrollX,
int scrollY,
int contentWidth,
int contentHeight,
int scrollViewWidth,
int scrollViewHeight) {
super.init(viewTag, timestampMs);
mScrollX = scrollX;
mScrollY = scrollY;
mContentWidth = contentWidth;