android RecyclerView 和 nestedscrollview

android RecyclerView and nestedscrollview

如何创建图片布局?

我在 mainActivity 中尝试过这个:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/htab_maincontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/htab_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

        <ImageView
            android:id="@+id/htab_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/header"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/htab_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="top"
            android:minHeight="?attr/actionBarSize"
            app:layout_collapseMode="parallax"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleMarginTop="13dp" />

        <android.support.design.widget.TabLayout
            android:id="@+id/htab_tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginTop="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            android:theme="@style/MyCustomTabLayout"
            app:tabIndicatorColor="@android:color/white" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/main_title"
            android:layout_gravity="bottom"
            android:layout_marginBottom="10dp"
            android:layout_marginStart="20dp"
            android:gravity="start"
            android:textStyle="bold"
            android:textSize="20dp"
            android:textColor="@color/colorAccent"
            app:layout_collapseMode="parallax"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:maxLines="1"
            android:text="test articles"
            android:layout_marginLeft="20dp" />
    </android.support.design.widget.CollapsingToolbarLayout>

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

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

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

而这个在 viewPager 中占位符的片段中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dummyfrag_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/background_material_light"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent">

    <android.support.v7.widget.RecyclerView
         android:id="@+id/scrollableview"
         android:layout_width="match_parent"
         android:layout_height="110dp"/>


</LinearLayout>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:columnWidth="90dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center"
        android:stretchMode="columnWidth"
        android:nestedScrollingEnabled="false"/>
</LinearLayout>

问题是:网格视图滚动不会折叠应用栏。 我想在网格视图滚动时折叠应用栏和水平 RecyclerView。

GridView 已经内置了滚动,因此它与 NestedScrollView 冲突。您应该使用带有 GridLayoutManagerappbar_scrolling_view_behavior 布局行为的 RecyclerView 来代替 NestedScrollView.
这个答案来自 here