recycler view not scrolling inside bottom sheet dialog

143 Views Asked by At

I have structure below

<LinearLayout>
   <ViewPager>
</LinearLayout>

inside ViewPager fragment layout

<LinearLayout android:orientation=vertical>
   <RecyclerView android:nestedScrollingEnabled=false />
   <Button />
</LinearLayout>

But nestedScrollingEnabled doesn't work from xml. But when I execute code below in bottom sheet fragment, it works, I don't know why

    try {
        val field = ViewPager2::class.java.getDeclaredField("mRecyclerView")
        field.isAccessible = true
        (field[binding.pager] as RecyclerView).isNestedScrollingEnabled = false
    } catch (e: NoSuchFieldException) {
        Timber.e(e)
    } catch (e: IllegalAccessException) {
        Timber.e(e)
    }
2

There are 2 best solutions below

0
nasibeyyubov On BEST ANSWER
pager.children.find { it is RecyclerView }?.let {
    (it as RecyclerView).isNestedScrollingEnabled = false
}

this code solved the problem without using reflection

0
Hezy Ziv On

NestedScrollView: Wrap your ViewPager2 inside a NestedScrollView. This will allow the parent NestedScrollView to handle the scrolling, and you can disable nested scrolling on the ViewPager2:

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="false" />

</androidx.core.widget.NestedScrollView>