android - 如何将 RecyclerView 与 NestedScrollView 一起使用?

标签 android listview layout android-recyclerview

我想在 LinearLayout 上滚动并在 RecyclerView 上禁用滚动。我尝试将 LinearLayout 更改为 NestedScrollView 但它不起作用,我不知道为什么。

我试过了 this question但效果不佳。 RecyclerView 中的内容可以从 WS 动态加载,并且它是一个无休止的滚动。 RecyclerView 中的内容更新时屏幕卡住。

我在推特上看到了这个,我们该怎么做 https://drive.google.com/file/d/0B2JZggeoXKKFdG1ENmZEdWFIa0k/view?usp=sharing

例子
这是我的应用程序的简单屏幕,我当前想在 red 滚动,而它当前在“blue”滚动。非常感谢。

enter image description here

代码

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

<ImageView
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:layout_margin="16dp"
    android:background="#CCC"
    android:id="@+id/imageView2" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Image caption"
    android:background="#cbffa3"
    android:padding="16dp"
    android:id="@+id/textView8" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

试过但不行,RecyclerView 中的内容不显示

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:layout_margin="16dp"
            android:background="#CCC"
            android:id="@+id/imageView2" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Image caption"
            android:background="#cbffa3"
            android:padding="16dp"
            android:id="@+id/textView8" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

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

最佳答案

选项 1:在 RecyclerView 上使用 WRAP_CONTENT 并将其与其他小部件一起放在 ScrollView 中。 RecyclerView 上的 WRAP_CONTENT 仅适用于 23.2尽管。这不是推荐的方法,因为如果使用 WRAP_CONTENT,您的 RecyclerView 项目将不会被回收,并且当内容变长时它会减慢 UI。

选项 2:为回收器 View 适配器使用类型并为索引 0 呈现不同的 View 。This答案显示了如何去做。

选项 3:实现这种体验的最佳选择是使用 CollapsingToolbarLayout。当您向上滚动时,您的顶部内容将折叠起来,您将能够滚动 RecyclerView。

这是使用 CollapsingToolbarLayout 复制 Twitter 页面的一种方法。

<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:layout_gravity="center"
    android:fitsSystemWindows="false"
    android:orientation="vertical"
    app:statusBarBackground="@android:color/transparent">


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

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="?actionBarSize"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@android:drawable/btn_star_big_on"
                app:layout_collapseMode="none"
                />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                android:background="@android:color/transparent"
                android:elevation="5dp"
                android:importantForAccessibility="yes"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="exitUntilCollapsed">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="?actionBarSize"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center_horizontal"
                    android:background="@android:color/white"
                    android:text="Title Here"
                    />
            </android.support.v7.widget.Toolbar>

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

有关此内容的更多信息,请参阅 Android 博客 - http://android-developers.blogspot.ca/2015/05/android-design-support-library.html (滚动到“CoordinatorLayout 和应用栏”)

关于android - 如何将 RecyclerView 与 NestedScrollView 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35962572/

相关文章:

android - 用android绘制可触摸层

android build.gradle 模块应用程序错误 : Failed to resolve: annotation

Android - 扩充 ListView

html - 固定 - 液体 - 固定布局

ios - 不带自动布局的 UILabel 自动框架高度

php - 如何将 android 客户端连接到笔记本电脑内的 Apache 服务器 (php) 的本地主机?

android - 想在 AVD 上的 sdcard 中创建文件夹

ios - Xamarin 窗体 : How to change background from gray on selected item in listview, iOS

ASP.Net ListView 按数据字段分组?

java - Java 中的嵌套选项卡