Skip to content

Commit

Permalink
Add OnLayoutChange API for scroll views (#47177)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47177

Changelog: [Android][Added] Add OnLayoutChange API for scroll views

Reviewed By: lyahdav, mdvacca

Differential Revision: D64843826

fbshipit-source-id: 3161730ccd0b3816d1704d07a02714a2ff04ae3a
  • Loading branch information
Thomas Nardone authored and facebook-github-bot committed Oct 24, 2024
1 parent ba061a5 commit d825a4d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
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

0 comments on commit d825a4d

Please sign in to comment.