From a713516eb2fdeb5995523d95dfea3a2e4a5fd538 Mon Sep 17 00:00:00 2001 From: "Adam.Katz" Date: Thu, 21 Dec 2023 23:01:50 +0200 Subject: [PATCH] feat: added sensitivity controller to android --- .../reactnativepagerview/PagerViewViewManagerImpl.kt | 11 +++++++++++ .../com/reactnativepagerview/PagerViewViewManager.kt | 7 +++++++ src/PagerViewNativeComponent.ts | 3 +++ 3 files changed, 21 insertions(+) diff --git a/android/src/main/java/com/reactnativepagerview/PagerViewViewManagerImpl.kt b/android/src/main/java/com/reactnativepagerview/PagerViewViewManagerImpl.kt index 0abf6682..39afb909 100644 --- a/android/src/main/java/com/reactnativepagerview/PagerViewViewManagerImpl.kt +++ b/android/src/main/java/com/reactnativepagerview/PagerViewViewManagerImpl.kt @@ -3,6 +3,7 @@ package com.reactnativepagerview import android.view.View import androidx.viewpager2.widget.ViewPager2 import com.facebook.react.uimanager.PixelUtil +import androidx.recyclerview.widget.RecyclerView object PagerViewViewManagerImpl { const val NAME = "RNCViewPager" @@ -20,6 +21,16 @@ object PagerViewViewManagerImpl { view.setCurrentItem(selectedTab, scrollSmooth) } + fun ViewPager2.reduceDragSensitivity(value: Int) { + val recyclerViewField = ViewPager2::class.java.getDeclaredField("mRecyclerView") + recyclerViewField.isAccessible = true + val recyclerView = recyclerViewField.get(this) as RecyclerView + val touchSlopField = RecyclerView::class.java.getDeclaredField("mTouchSlop") + touchSlopField.isAccessible = true + val touchSlop = touchSlopField.get(recyclerView) as Int + touchSlopField.set(recyclerView, touchSlop*value) + } + fun addView(host: NestedScrollableHost, child: View?, index: Int) { if (child == null) { return diff --git a/android/src/paper/java/com/reactnativepagerview/PagerViewViewManager.kt b/android/src/paper/java/com/reactnativepagerview/PagerViewViewManager.kt index 4d0adffe..1ed255af 100644 --- a/android/src/paper/java/com/reactnativepagerview/PagerViewViewManager.kt +++ b/android/src/paper/java/com/reactnativepagerview/PagerViewViewManager.kt @@ -13,6 +13,7 @@ import com.facebook.react.uimanager.UIManagerModule import com.facebook.react.uimanager.ViewGroupManager import com.facebook.react.uimanager.annotations.ReactProp import com.facebook.react.uimanager.events.EventDispatcher +import com.reactnativepagerview.PagerViewViewManagerImpl.reduceDragSensitivity import com.reactnativepagerview.event.PageScrollEvent import com.reactnativepagerview.event.PageScrollStateChangedEvent import com.reactnativepagerview.event.PageSelectedEvent @@ -125,6 +126,12 @@ class PagerViewViewManager : ViewGroupManager() { PagerViewViewManagerImpl.setLayoutDirection(host, value) } + @ReactProp(name = "scrollSensitivity", defaultInt = 2) + fun setScrollSensitivity(host: NestedScrollableHost, value: Int) { + val view = PagerViewViewManagerImpl.getViewPager(host) + view.reduceDragSensitivity(value) + } + override fun getExportedCustomDirectEventTypeConstants(): MutableMap> { return MapBuilder.of( PageScrollEvent.EVENT_NAME, MapBuilder.of("registrationName", "onPageScroll"), diff --git a/src/PagerViewNativeComponent.ts b/src/PagerViewNativeComponent.ts index f102aa96..7fe3ab63 100644 --- a/src/PagerViewNativeComponent.ts +++ b/src/PagerViewNativeComponent.ts @@ -22,6 +22,8 @@ export type OnPageScrollStateChangedEventData = Readonly<{ pageScrollState: 'idle' | 'dragging' | 'settling'; }>; +export type ScrollSensitivity = 2 | 3 | 4 | 5 | 6 | 7 | 8; + export interface NativeProps extends ViewProps { scrollEnabled?: WithDefault; layoutDirection?: WithDefault<'ltr' | 'rtl', 'ltr'>; @@ -35,6 +37,7 @@ export interface NativeProps extends ViewProps { onPageScroll?: DirectEventHandler; onPageSelected?: DirectEventHandler; onPageScrollStateChanged?: DirectEventHandler; + scrollSensitivity?: ScrollSensitivity; } type PagerViewViewType = HostComponent;