From 3541fb188b5294e3cfc04b94982bb8a5d0040b21 Mon Sep 17 00:00:00 2001 From: vishtree Date: Thu, 8 Apr 2021 16:32:18 +0200 Subject: [PATCH] Fixed some logic around enabling mvcp --- .../MvcpScrollViewManagerModule.java | 29 +++++++++++-------- src/FlatList.android.tsx | 7 ++--- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/android/src/main/java/com/mvcpscrollviewmanager/MvcpScrollViewManagerModule.java b/android/src/main/java/com/mvcpscrollviewmanager/MvcpScrollViewManagerModule.java index fa0ec8e..56b6842 100644 --- a/android/src/main/java/com/mvcpscrollviewmanager/MvcpScrollViewManagerModule.java +++ b/android/src/main/java/com/mvcpscrollviewmanager/MvcpScrollViewManagerModule.java @@ -58,19 +58,24 @@ public void run() { final UIManagerModuleListener uiManagerModuleListener = new UIManagerModuleListener() { @Override public void willDispatchViewUpdates(final UIManagerModule uiManagerModule) { - ReactViewGroup mContentView = (ReactViewGroup)scrollView.getChildAt(0); - if (mContentView == null) return; - - ScrollViewUIHolders.currentScrollY = scrollView.getScrollY(); - - for (int ii = minIndexForVisible; ii < mContentView.getChildCount(); ++ii) { - View subview = mContentView.getChildAt(ii); - if (subview.getTop() >= ScrollViewUIHolders.currentScrollY) { - ScrollViewUIHolders.prevFirstVisibleTop = subview.getTop(); - ScrollViewUIHolders.firstVisibleView = subview; - break; + uiManagerModule.prependUIBlock(new UIBlock() { + @Override + public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) { + ReactViewGroup mContentView = (ReactViewGroup)scrollView.getChildAt(0); + if (mContentView == null) return; + + ScrollViewUIHolders.currentScrollY = scrollView.getScrollY(); + + for (int ii = minIndexForVisible; ii < mContentView.getChildCount(); ++ii) { + View subview = mContentView.getChildAt(ii); + if (subview.getTop() >= ScrollViewUIHolders.currentScrollY) { + ScrollViewUIHolders.prevFirstVisibleTop = subview.getTop(); + ScrollViewUIHolders.firstVisibleView = subview; + break; + } + } } - } + }); } }; diff --git a/src/FlatList.android.tsx b/src/FlatList.android.tsx index 299d328..3f2f81e 100644 --- a/src/FlatList.android.tsx +++ b/src/FlatList.android.tsx @@ -23,11 +23,9 @@ export default (React.forwardRef( const propAutoscrollToTopThreshold = mvcp?.autoscrollToTopThreshold || -Number.MAX_SAFE_INTEGER; const propMinIndexForVisible = mvcp?.minIndexForVisible || 1; - const hasMvcpChanged = autoscrollToTopThreshold.current !== propAutoscrollToTopThreshold || minIndexForVisible.current !== propMinIndexForVisible; - const enableMvcp = () => { if (!flRef.current) return; @@ -45,9 +43,6 @@ export default (React.forwardRef( }; const enableMvcpWithRetries = () => { - autoscrollToTopThreshold.current = propAutoscrollToTopThreshold; - minIndexForVisible.current = propMinIndexForVisible; - return enableMvcp()?.catch(() => { /** * enableMaintainVisibleContentPosition from native module may throw IllegalViewOperationException, @@ -86,6 +81,8 @@ export default (React.forwardRef( if (isMvcpEnabled.current && !hasMvcpChanged) { return; } + autoscrollToTopThreshold.current = propAutoscrollToTopThreshold; + minIndexForVisible.current = propMinIndexForVisible; isMvcpEnabled.current = true; disableMvcp().then(enableMvcpWithRetries);