Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added sensitivity controller to android #802

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -125,6 +126,12 @@ class PagerViewViewManager : ViewGroupManager<NestedScrollableHost>() {
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<String, Map<String, String>> {
return MapBuilder.of(
PageScrollEvent.EVENT_NAME, MapBuilder.of("registrationName", "onPageScroll"),
Expand Down
3 changes: 3 additions & 0 deletions src/PagerViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean, true>;
layoutDirection?: WithDefault<'ltr' | 'rtl', 'ltr'>;
Expand All @@ -35,6 +37,7 @@ export interface NativeProps extends ViewProps {
onPageScroll?: DirectEventHandler<OnPageScrollEventData>;
onPageSelected?: DirectEventHandler<OnPageSelectedEventData>;
onPageScrollStateChanged?: DirectEventHandler<OnPageScrollStateChangedEventData>;
scrollSensitivity?: ScrollSensitivity;
}

type PagerViewViewType = HostComponent<NativeProps>;
Expand Down
Loading