Browse Source

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

ifsnow 5 years ago
parent
commit
d8743eeed2

+ 19
- 1
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

435
     view.getSettings().setGeolocationEnabled(isGeolocationEnabled != null && isGeolocationEnabled);
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
   @Override
443
   @Override
439
   protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
444
   protected void addEventEmitters(ThemedReactContext reactContext, WebView view) {
440
     // Do not register default touch emitter and let WebView implementation handle touches
445
     // Do not register default touch emitter and let WebView implementation handle touches
784
     protected @Nullable
789
     protected @Nullable
785
     RNCWebViewClient mRNCWebViewClient;
790
     RNCWebViewClient mRNCWebViewClient;
786
     protected boolean sendContentSizeChangeEvents = false;
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
      * WebView must be created with an context of the current activity
796
      * WebView must be created with an context of the current activity
800
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
806
       this.sendContentSizeChangeEvents = sendContentSizeChangeEvents;
801
     }
807
     }
802
 
808
 
809
+    public void setHasScrollEvent(boolean hasScrollEvent) {
810
+      this.hasScrollEvent = hasScrollEvent;
811
+    }
812
+
803
     @Override
813
     @Override
804
     public void onHostResume() {
814
     public void onHostResume() {
805
       // do nothing
815
       // do nothing
894
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {
904
     protected void onScrollChanged(int x, int y, int oldX, int oldY) {
895
       super.onScrollChanged(x, y, oldX, oldY);
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
       if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
915
       if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
898
         ScrollEvent event = ScrollEvent.obtain(
916
         ScrollEvent event = ScrollEvent.obtain(
899
                 this.getId(),
917
                 this.getId(),