Home » Android » android – Recycler view + View Pager + Nested ScrollView + CoordinatorLayout

android – Recycler view + View Pager + Nested ScrollView + CoordinatorLayout

Posted by: admin June 15, 2020 Leave a comment

Questions:

enter image description hereI am working on a layout such that the ViewPager holding a fragment that contains recycler view is placed in a nester scrollView of Coordinator Layout.

The problem is once i clicked on the recycler view the nested scroll view is not working..

Is there any possible way to have a layout like these that also scrolls

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/lin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ViewPager
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />


        </LinearLayout>


    <NestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="190dp"
                android:minHeight="190dp"
                app:layout_collapseMode="parallax" >

                <ViewFlipper
                    android:id="@+id/view_flipper"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <!--<ImageView
                        android:id="@+id/backdrop"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:adjustViewBounds="true"
                        android:fitsSystemWindows="true"
                        android:scaleType="centerCrop"/>-->

                </ViewFlipper>

            </FrameLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                app:layout_collapseMode="pin" />

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="?attr/colorPrimary" />

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
How to&Answers:

The ViewPager doesn’t work in a NestedScrollView. You have to use it as a child of the CoordinatorLayout.

<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">

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="190dp"
            android:minHeight="190dp"
            app:layout_collapseMode="parallax">

            <ViewFlipper
                android:id="@+id/view_flipper"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <!--<ImageView
                    android:id="@+id/backdrop"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"/>-->

            </ViewFlipper>

        </FrameLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:layout_collapseMode="pin" />

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?attr/colorPrimary" />

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

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

Answer:

Don’t use 「Recycler view View Pager + NestedScrollView + CoordinatorLayout」 at the same time,just remove the NestedScrollView it’s works fine.

 <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/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_collapseParallaxMultiplier="0.7"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false">

            <LinearLayout
                android:id="@+id/header_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:fitsSystemWindows="true"
                android:orientation="vertical"
                app:layout_collapseParallaxMultiplier="0.7">

              <..somethings..>

            </LinearLayout>

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:visibility="invisible"
                app:layout_collapseMode="pin"/>
        </android.support.design.widget.CollapsingToolbarLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_gravity="bottom"
            android:orientation="vertical">

            <View
                android:layout_width="wrap_content"
                android:layout_height="0.4dp"
                android:background="?attr/separate_line_color"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/tablayout_user"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"

                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view_line"
                app:layout_scrollFlags="enterAlways"
                app:tabGravity="center"
                android:background="?attr/common_bg_white_black"
                app:tabIndicatorColor="@color/theme_color"
                app:tabIndicatorHeight="2dp"
                app:tabMaxWidth="0dp"
                app:tabMode="fixed"
                app:tabPaddingEnd="20dp"
                app:tabPaddingStart="20dp"
                app:tabSelectedTextColor="@color/theme_color"
                app:tabTextAppearance="@style/TabTextAppearance2"
                app:tabTextColor="?attr/text_color_2"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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