Skip to content

Commit

Permalink
Add snapToAlignment to ReactHorizontalScrollViewManager
Browse files Browse the repository at this point in the history
Summary:
This diff adds the new snapToAlignment into ReactHorizontalScrollViewManager

changelog: [Android][Added] Implement snapToAlignment into ReactHorizontalScrollViewManager

Reviewed By: JoshuaGross

Differential Revision: D31174545

fbshipit-source-id: c56bfca207980428be98dd21a5387a0ff6bcd296
  • Loading branch information
mdvacca authored and facebook-github-bot committed Sep 29, 2021
1 parent a54cfb9 commit deec1db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.facebook.react.views.scroll;

import static com.facebook.react.views.scroll.ReactScrollViewHelper.SNAP_ALIGNMENT_DISABLED;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
Expand Down Expand Up @@ -94,6 +96,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView
private @Nullable List<Integer> mSnapOffsets;
private boolean mSnapToStart = true;
private boolean mSnapToEnd = true;
private int mSnapToAlignment = SNAP_ALIGNMENT_DISABLED;
private ReactViewBackgroundManager mReactBackgroundManager;
private boolean mPagedArrowScrolling = false;
private int pendingContentOffsetX = UNSET_CONTENT_OFFSET;
Expand Down Expand Up @@ -239,6 +242,10 @@ public void setSnapToEnd(boolean snapToEnd) {
mSnapToEnd = snapToEnd;
}

public void setSnapToAlignment(int snapToAlignment) {
mSnapToAlignment = snapToAlignment;
}

public void flashScrollIndicators() {
awakenScrollBars();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public void setSnapToInterval(ReactHorizontalScrollView view, float snapToInterv
view.setSnapInterval((int) (snapToInterval * screenDisplayMetrics.density));
}

@ReactProp(name = "snapToAlignment")
public void setSnapToAlignment(ReactHorizontalScrollView view, String alignment) {
view.setSnapToAlignment(ReactScrollViewHelper.parseSnapToAlignment(alignment));
}

@ReactProp(name = "snapToOffsets")
public void setSnapToOffsets(
ReactHorizontalScrollView view, @Nullable ReadableArray snapToOffsets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.OverScroller;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.UIManagerHelper;
Expand All @@ -26,6 +27,11 @@ public class ReactScrollViewHelper {
public static final String AUTO = "auto";
public static final String OVER_SCROLL_NEVER = "never";

public static final int SNAP_ALIGNMENT_DISABLED = 0;
public static final int SNAP_ALIGNMENT_START = 1;
public static final int SNAP_ALIGNMENT_CENTER = 2;
public static final int SNAP_ALIGNMENT_END = 3;

public interface ScrollListener {
void onScroll(
ViewGroup scrollView, ScrollEventType scrollEventType, float xVelocity, float yVelocity);
Expand Down Expand Up @@ -119,6 +125,20 @@ public static int parseOverScrollMode(String jsOverScrollMode) {
}
}

public static int parseSnapToAlignment(@Nullable String alignment) {
if (alignment == null) {
return SNAP_ALIGNMENT_DISABLED;
} else if ("start".equalsIgnoreCase(alignment)) {
return SNAP_ALIGNMENT_START;
} else if ("center".equalsIgnoreCase(alignment)) {
return SNAP_ALIGNMENT_CENTER;
} else if ("end".equals(alignment)) {
return SNAP_ALIGNMENT_END;
} else {
throw new JSApplicationIllegalArgumentException("wrong snap alignment value: " + alignment);
}
}

public static int getDefaultScrollAnimationDuration(Context context) {
if (!mSmoothScrollDurationInitialized) {
mSmoothScrollDurationInitialized = true;
Expand Down

0 comments on commit deec1db

Please sign in to comment.