android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.
Apache License 2.0
5.04k stars 3.01k forks source link

ViewPager2 horizontal scrolling conflict with the WebView child #154

Open imrankst1221 opened 4 years ago

imrankst1221 commented 4 years ago

MainActivity

 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_below="@+id/layout_toolbar">
                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tab_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="top"
                    app:tabMode="scrollable" />
                <androidx.viewpager2.widget.ViewPager2
                    android:id="@+id/view_pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:nestedScrollingEnabled="false" />
            </LinearLayout>

Fragment

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="infix.imrankst1221.webapp.ui.home.WebViewFragment">
    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:windowSoftInputMode="adjustResize"/>
</FrameLayout>
sam-devstudio commented 3 years ago

@imrankst1221 This is an issue in ViewPager2 reported at https://issuetracker.google.com/issues/123006042

ali-sardari commented 2 years ago

To fix this, you should extend the ViewPager2 like this:

fun ViewPager2.reduceDragSensitivity() {
    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*8)       // "8" was obtained experimentally
}

and call reduceDragSensitivity. So the viewpager2 will look like this:

view_pager.reduceDragSensitivity()

Example: https://github.com/ali-sardari/ViewPager2Example

dengfa commented 3 months ago

mark