Mahavir
Mahavir

Reputation: 29

Collapsing Layout Behaviour Not working Properly inside vertical view pager

View pager ScreenShot

enter image description here

When i scroll (Main)recycler view Collapsing toolbar should expand and collapse and after vertical view pager should scroll.

But in my code (main)Recycler view not scrolling properly and collapsing toolbar not working properly.

Xml:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/MyAppbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                app:layout_collapseMode="none">

                <TextView... />

                <ProfileView... />

            </LinearLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/fragment_snip_details_2view"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_40sdp"
                android:background="@color/colorAccent"
                android:minHeight="@dimen/_40sdp"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>

        <TabLayout...>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="visible"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/fragment_snip_details_2_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

Upvotes: 1

Views: 1167

Answers (1)

NaturallyAsh
NaturallyAsh

Reputation: 85

wrap your viewpager inside a NestedScrollView instead of a LinearLayout and then set the layout_behavior to appbar_scrolling_view for both the NestedScrollView and the ViewPager. Took me forever but I found that this worked for me.

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <com.example.ashleighwilson.schoolscheduler.views.CustomViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </com.example.ashleighwilson.schoolscheduler.views.CustomViewPager>

</android.support.v4.widget.NestedScrollView>

also be sure to set fillViewport to true in the NestedScrollView and make sure the width and height is set to match parent. This is important.

Upvotes: 2

Related Questions