Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snapToInterval support for Android ScrollView #15667

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Libraries/Components/ScrollView/ScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,6 @@ const ScrollView = createReactClass({
* that have lengths smaller than the scroll view. Typically used in
* combination with `snapToAlignment` and `decelerationRate="fast"`.
* Overrides less configurable `pagingEnabled` prop.
*
* @platform ios
*/
snapToInterval: PropTypes.number,
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.widget.HorizontalScrollView;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.uimanager.MeasureSpecAssertions;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactClippingViewGroup;
import com.facebook.react.uimanager.ReactClippingViewGroupHelper;
import com.facebook.react.uimanager.events.NativeGestureUtil;
Expand All @@ -41,6 +42,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView implements
private @Nullable Rect mClippingRect;
private boolean mDragging;
private boolean mPagingEnabled = false;
private double mSnapToInterval;
private @Nullable Runnable mPostTouchRunnable;
private boolean mRemoveClippedSubviews;
private boolean mScrollEnabled = true;
Expand Down Expand Up @@ -94,6 +96,10 @@ public void flashScrollIndicators() {
awakenScrollBars();
}

public void setSnapToInterval(double snapToInterval) {
mSnapToInterval = snapToInterval;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
MeasureSpecAssertions.assertExplicitMeasureSpec(widthMeasureSpec, heightMeasureSpec);
Expand Down Expand Up @@ -235,6 +241,13 @@ private void disableFpsListener() {
}
}

private int getPageWidth() {
if(mSnapToInterval != 0) {
return (int) (PixelUtil.toPixelFromDIP(mSnapToInterval) + 0.5);
}
return getWidth();
}

private boolean isScrollPerfLoggingEnabled() {
return mFpsListener != null && mScrollPerfTag != null && !mScrollPerfTag.isEmpty();
}
Expand Down Expand Up @@ -312,7 +325,7 @@ public void run() {
* scrolling.
*/
private void smoothScrollToPage(int velocity) {
int width = getWidth();
int width = getPageWidth();
int currentX = getScrollX();
// TODO (t11123799) - Should we do anything beyond linear accounting of the velocity
int predictedX = currentX + velocity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void setPagingEnabled(ReactHorizontalScrollView view, boolean pagingEnabl
view.setPagingEnabled(pagingEnabled);
}

@ReactProp(name = "snapToInterval")
public void setSnapToInterval(ReactHorizontalScrollView view, double snapToInterval) {
view.setSnapToInterval(snapToInterval);
}

/**
* Controls overScroll behaviour
*/
Expand Down