laenger / ViewPagerBottomSheet

Use ViewPagers in Bottom Sheets!
Apache License 2.0
465 stars 86 forks source link

Nested scrolling doesn't work #5

Closed bogdanzurac closed 7 years ago

bogdanzurac commented 7 years ago

I'm trying to get this library working with a ViewPager inside a LinearLayout with BottomSheetBehavior, which contains fragments with a NestedScrollView inside a RelativeLayout. Layouts are below. Everything works as expected, apart from the actual nested scrolling. Is there something that I'm missing? At first I thought the additional OnPageChangedListener was the issue, but even after removing it, it still didn't work.

layout/activity_main.xml

<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
            android:id="@+id/activity_bottom_sheet_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/color_white"
            android:elevation="@dimen/design_appbar_elevation"
            app:behavior_peekHeight="@dimen/height_bottom_sheet"
            app:layout_behavior="@string/view_pager_bottom_sheet_behavior">

        <android.support.v7.widget.RecyclerView
                android:id="@+id/activity_bottom_sheet_recyclerview"
                android:layout_width="match_parent"
                android:layout_height="@dimen/height_bottom_sheet"
                android:clipToPadding="false"
                android:orientation="horizontal"
                android:background="@color/color_primary"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

        <android.support.v4.view.ViewPager
                android:id="@+id/activity_bottom_sheet_viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

layout/fragment_nested.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/color_white">

    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical">
    </android.support.v4.widget.NestedScrollView>
</RelativeLayout>

java/MainActivity.java

 @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      mViewPager = (ViewPager) findViewById(R.id.activity_bottom_sheet_viewpager);         mViewPager.addOnPageChangeListener(mOnViewPagerPageChangeListener);
       BottomSheetUtils.setupViewPager(mViewPager);
}
laenger commented 7 years ago

I believe Bottom Sheets by default, and this library doesn't change this, only support one scrollable child at a time. And when I look at your activity_main.xml I see a RecyclerView just above the ViewPager which is probably picked up as scrolling child before the ViewPager is even considered. I would guess that moving the ViewPager up in the hierarchy would change that but then of course you would lose nested scrolling on the RecyclerView. Is simplifying your screen design an option?

bogdanzurac commented 7 years ago

I can confirm that moving the ViewPager above the RecyclerView enables nested scrolling. Really nice catch, thanks a lot for pointing that out! In my case, I didn't need nested scrolling on the RecyclerView, since it has a horizontal layout manager.

Closing this, since the library works as intended. For anybody reading this, just remember to place the View that requires nested scrolling as the first element.