当触摸到工具栏布局时,嵌套滚动视图滚动其中的内容而不是扩展其布局大小

Nestedscroll view scrolls content inside it instead of expanding its layout size when touch to toolbar layout

在 Nestedscrollview 中的 Coordinatorlayout 中,我有 RelativeLayout,在 RelativeLayout 中,我有 Textview。当 Nestedscrollview 接触到工具栏底部时,它会滚动文本视图内容而不是完整的 Relativelayout 卡。

这是我的代码。

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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="192dp"
            android:fitsSystemWindows="true"
            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="@color/white"
                app:expandedTitleTextAppearance="@style/TransparentText"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:titleEnabled="false">

                   <ImageView
                    android:id="@+id/product_img"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:fitsSystemWindows="true"
                    app:imageUrl="@{viewModel.imageUrl}"
                    android:scaleType="centerInside"
                    app:layout_collapseMode="parallax"
                    android:background="@color/white"/>

                   <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:elevation="4dp"
                    android:gravity="center_vertical"
                    android:minHeight="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:theme="@style/ThemeOverlay.AppCompat.Light"> 

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

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

           <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/dimen_60"
            android:layout_marginLeft="@dimen/dimen_7"
            android:layout_marginRight="@dimen/dimen_7"
            android:layout_marginTop="@dimen/dimen_7"
            android:background="@drawable/card_bg"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

               <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/dimen_3"
                android:layout_marginRight="@dimen/dimen_3"
                android:layout_marginTop="@dimen/dimen_3"
                android:background="@color/white">

                <app.com.test.Views.CustomTextView
                    android:id="@+id/description_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/dimen_12"
                    android:layout_marginRight="@dimen/dimen_12"
                    android:layout_marginTop="@dimen/dimen_12"
                    android:lineSpacingExtra="8dp"
                    android:textColor="@color/title_color"
                    android:textSize="14sp"/>

                 </RelativeLayout>

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

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

我得到了解决方案。实际上 NestedScrollView 不会滚动它的子元素,它实际上会滚动它的子元素内部的内容,因此要滚动整个布局而不是文本,我们需要再添加一个父布局,如下所示:

           <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"
            android:layout_marginBottom="@dimen/dimen_60"
            android:scrollbars="none"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/dimen_10"
                android:background="@color/bg_color"
                android:orientation="vertical"
                android:padding="@dimen/dimen_6">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/dimen_10"
                    android:layout_marginLeft="@dimen/dimen_3"
                    android:layout_marginRight="@dimen/dimen_3"
                    android:layout_marginTop="@dimen/dimen_3"
                    android:background="@drawable/card_bg">

                    <app.com.test.Views.CustomTextView
                        android:id="@+id/product_title_txt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/dimen_12"
                        android:layout_marginRight="@dimen/dimen_12"
                        android:layout_marginTop="@dimen/dimen_12"
                        android:text=""
                     />
             </RelativeLayout>
           </LinearLayout>
          </android.support.v4.widget.NestedScrollView>