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 OnLayoutChange API for scroll views #47177

Closed
wants to merge 1 commit into from
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
7 changes: 7 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -7248,7 +7248,9 @@ public final class com/facebook/react/views/scroll/ReactScrollViewHelper {
public static final field SNAP_ALIGNMENT_DISABLED I
public static final field SNAP_ALIGNMENT_END I
public static final field SNAP_ALIGNMENT_START I
public static final fun addLayoutChangeListener (Lcom/facebook/react/views/scroll/ReactScrollViewHelper$LayoutChangeListener;)V
public static final fun addScrollListener (Lcom/facebook/react/views/scroll/ReactScrollViewHelper$ScrollListener;)V
public static final fun emitLayoutChangeEvent (Landroid/view/ViewGroup;)V
public static final fun emitLayoutEvent (Landroid/view/ViewGroup;)V
public static final fun emitScrollBeginDragEvent (Landroid/view/ViewGroup;)V
public static final fun emitScrollEndDragEvent (Landroid/view/ViewGroup;FF)V
Expand All @@ -7262,6 +7264,7 @@ public final class com/facebook/react/views/scroll/ReactScrollViewHelper {
public static final fun parseSnapToAlignment (Ljava/lang/String;)I
public static final fun predictFinalScrollPosition (Landroid/view/ViewGroup;IIII)Landroid/graphics/Point;
public final fun registerFlingAnimator (Landroid/view/ViewGroup;)V
public static final fun removeLayoutChangeListener (Lcom/facebook/react/views/scroll/ReactScrollViewHelper$LayoutChangeListener;)V
public static final fun removeScrollListener (Lcom/facebook/react/views/scroll/ReactScrollViewHelper$ScrollListener;)V
public static final fun smoothScrollTo (Landroid/view/ViewGroup;II)V
public static final fun updateFabricScrollState (Landroid/view/ViewGroup;)V
Expand Down Expand Up @@ -7296,6 +7299,10 @@ public abstract interface class com/facebook/react/views/scroll/ReactScrollViewH
public abstract fun getStateWrapper ()Lcom/facebook/react/uimanager/StateWrapper;
}

public abstract interface class com/facebook/react/views/scroll/ReactScrollViewHelper$LayoutChangeListener {
public abstract fun onLayoutChange (Landroid/view/ViewGroup;)V
}

public final class com/facebook/react/views/scroll/ReactScrollViewHelper$ReactScrollViewScrollState {
public fun <init> (I)V
public final fun getDecelerationRate ()F
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,7 @@ public void onLayoutChange(
} else if (mMaintainVisibleContentPositionHelper != null) {
mMaintainVisibleContentPositionHelper.updateScrollPosition();
}
ReactScrollViewHelper.emitLayoutChangeEvent(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,8 @@ public void onLayoutChange(
scrollTo(getScrollX(), maxScrollY);
}
}

ReactScrollViewHelper.emitLayoutChangeEvent(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public object ReactScrollViewHelper {

// Support global native listeners for scroll events
private val scrollListeners = CopyOnWriteArrayList<WeakReference<ScrollListener>>()
private val layoutChangeListeners = CopyOnWriteArrayList<WeakReference<LayoutChangeListener>>()

// If all else fails, this is the hardcoded value in OverScroller.java, in AOSP.
// The default is defined here (as of this diff):
Expand Down Expand Up @@ -151,6 +152,13 @@ public object ReactScrollViewHelper {
}
}

@JvmStatic
public fun emitLayoutChangeEvent(scrollView: ViewGroup) {
for (listener in layoutChangeListeners) {
listener.get()?.onLayoutChange(scrollView)
}
}

@JvmStatic
public fun parseOverScrollMode(jsOverScrollMode: String?): Int {
return if (jsOverScrollMode == null || jsOverScrollMode == AUTO) {
Expand Down Expand Up @@ -214,6 +222,16 @@ public object ReactScrollViewHelper {
scrollListeners.remove(WeakReference(listener))
}

@JvmStatic
public fun addLayoutChangeListener(listener: LayoutChangeListener) {
layoutChangeListeners.add(WeakReference(listener))
}

@JvmStatic
public fun removeLayoutChangeListener(listener: LayoutChangeListener) {
layoutChangeListeners.remove(WeakReference(listener))
}

/**
* Scroll the given view to the location (x, y), with provided initial velocity. This method works
* by calculate the "would be" initial velocity with internal friction to move to the point (x,
Expand Down Expand Up @@ -456,6 +474,10 @@ public object ReactScrollViewHelper {
public fun onLayout(scrollView: ViewGroup?)
}

public interface LayoutChangeListener {
public fun onLayoutChange(scrollView: ViewGroup)
}

public interface HasStateWrapper {
public val stateWrapper: StateWrapper?
}
Expand Down
Loading