Parcourir la source

fix(performance): Improved `onScroll` to work efficiently on Android (#609)

ifsnow il y a 5 ans
Parent
révision
8989574f23

+ 19
- 1
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Voir le fichier

@@ -435,6 +435,11 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
435 435
     view.getSettings().setGeolocationEnabled(isGeolocationEnabled != null && isGeolocationEnabled);
436 436
   }
437 437
 
438
+  @ReactProp(name = "onScroll")
439
+  public void setOnScroll(WebView view, boolean hasScrollEvent) {
440
+    ((RNCWebView) view).setHasScrollEvent(hasScrollEvent);
441
+  }
442
+
438 443
   @Override
439 444
   protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
440 445
     // Do not register default touch emitter and let WebView implementation handle touches
@@ -784,7 +789,8 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
784 789
     protected @Nullable
785 790
     RNCWebViewClient mRNCWebViewClient;
786 791
     protected boolean sendContentSizeChangeEvents = false;
787
-    private final OnScrollDispatchHelper mOnScrollDispatchHelper = new OnScrollDispatchHelper();
792
+    private OnScrollDispatchHelper mOnScrollDispatchHelper;
793
+    protected boolean hasScrollEvent = false;
788 794
 
789 795
     /**
790 796
      * WebView must be created with an context of the current activity
@@ -800,6 +806,10 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
800 806
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
801 807
     }
802 808
 
809
+    public void setHasScrollEvent(boolean hasScrollEvent) {
810
+      this.hasScrollEvent = hasScrollEvent;
811
+    }
812
+
803 813
     @Override
804 814
     public void onHostResume() {
805 815
       // do nothing
@@ -894,6 +904,14 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
894 904
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {
895 905
       super.onScrollChanged(x, y, oldX, oldY);
896 906
 
907
+      if (!hasScrollEvent) {
908
+        return;
909
+      }
910
+
911
+      if (mOnScrollDispatchHelper == null) {
912
+        mOnScrollDispatchHelper = new OnScrollDispatchHelper();
913
+      }
914
+
897 915
       if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
898 916
         ScrollEvent event = ScrollEvent.obtain(
899 917
                 this.getId(),