support removeClippedSubviews in ReactHorizontalScrollView

Reviewed By: andreicoman11

Differential Revision: D2831013

fb-gh-sync-id: 2ac4eb8d748f2d66523992e8683c1b4d60198ba1
This commit is contained in:
Felix Oghina 2016-01-15 09:46:34 -08:00 committed by facebook-github-bot-7
parent 274a3e0585
commit a10a510d96
2 changed files with 67 additions and 1 deletions

View File

@ -9,29 +9,54 @@
package com.facebook.react.views.scroll;
import javax.annotation.Nullable;
import android.content.Context;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.uimanager.MeasureSpecAssertions;
import com.facebook.react.uimanager.events.NativeGestureUtil;
import com.facebook.react.views.view.ReactClippingViewGroup;
import com.facebook.react.views.view.ReactClippingViewGroupHelper;
/**
* Similar to {@link ReactScrollView} but only supports horizontal scrolling.
*/
public class ReactHorizontalScrollView extends HorizontalScrollView {
public class ReactHorizontalScrollView extends HorizontalScrollView implements
ReactClippingViewGroup {
private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper();
private boolean mRemoveClippedSubviews;
private boolean mSendMomentumEvents;
private boolean mDragging;
private boolean mFlinging;
private boolean mDoneFlinging;
private @Nullable Rect mClippingRect;
public ReactHorizontalScrollView(Context context) {
super(context);
}
@Override
public void setRemoveClippedSubviews(boolean removeClippedSubviews) {
if (removeClippedSubviews && mClippingRect == null) {
mClippingRect = new Rect();
}
mRemoveClippedSubviews = removeClippedSubviews;
updateClippingRect();
}
@Override
public boolean getRemoveClippedSubviews() {
return mRemoveClippedSubviews;
}
public void setSendMomentumEvents(boolean sendMomentumEvents) {
mSendMomentumEvents = sendMomentumEvents;
}
@ -56,9 +81,14 @@ public class ReactHorizontalScrollView extends HorizontalScrollView {
super.onScrollChanged(x, y, oldX, oldY);
if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
if (mRemoveClippedSubviews) {
updateClippingRect();
}
if (mFlinging) {
mDoneFlinging = false;
}
ReactScrollViewHelper.emitScrollEvent(this);
}
}
@ -106,4 +136,33 @@ public class ReactHorizontalScrollView extends HorizontalScrollView {
postOnAnimationDelayed(r, ReactScrollViewHelper.MOMENTUM_DELAY);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mRemoveClippedSubviews) {
updateClippingRect();
}
}
@Override
public void updateClippingRect() {
if (!mRemoveClippedSubviews) {
return;
}
Assertions.assertNotNull(mClippingRect);
ReactClippingViewGroupHelper.calculateClippingRect(this, mClippingRect);
View contentView = getChildAt(0);
if (contentView instanceof ReactClippingViewGroup) {
((ReactClippingViewGroup) contentView).updateClippingRect();
}
}
@Override
public void getClippingRect(Rect outClippingRect) {
outClippingRect.set(Assertions.assertNotNull(mClippingRect));
}
}

View File

@ -15,6 +15,7 @@ import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.views.view.ReactClippingViewGroupHelper;
/**
* View manager for {@link ReactHorizontalScrollView} components.
@ -38,6 +39,12 @@ public class ReactHorizontalScrollViewManager
return new ReactHorizontalScrollView(context);
}
@ReactProp(name = ReactClippingViewGroupHelper.PROP_REMOVE_CLIPPED_SUBVIEWS)
public void setRemoveClippedSubviews(ReactHorizontalScrollView view, boolean removeClippedSubviews) {
view.setRemoveClippedSubviews(removeClippedSubviews);
}
@ReactProp(name = "showsHorizontalScrollIndicator")
public void setShowsHorizontalScrollIndicator(ReactHorizontalScrollView view, boolean value) {
view.setHorizontalScrollBarEnabled(value);