如何在NestedScrollView中使用RecyclerView?

3
我想在LinearLayout上实现滚动,并禁用RecyclerView的滚动。我尝试将LinearLayout更改为NestedScrollView,但它不起作用,我不知道为什么。
我尝试了这个问题,但效果不好。RecyclerView中的内容可以从WS动态加载,是无限滚动的。当RecyclerView中的内容更新时,屏幕会冻结。 我从Twitter上看到了这个,我们该怎么做呢?https://drive.google.com/file/d/0B2JZggeoXKKFdG1ENmZEdWFIa0k/view?usp=sharing

例子
这是我的应用程序的简单屏幕,我想要滚动到红色,但它目前滚动到了'蓝色'。非常感谢。

在此输入图像描述

代码

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

可能是重复的问题:如何在NestedScrollView中使用RecyclerView? - Rohit Arya
我更新了我的问题。 - Marcus
2个回答

4

选项1:在RecyclerView上使用WRAP_CONTENT,并将其放入滚动视图中与其他小部件一起。 但仅当使用23.2时,RecyclerView上的WRAP_CONTENT才有效。这不是推荐的方法,因为如果使用WRAP_CONTENT,则RecyclerView项目不会被循环利用,并且当内容变长时会导致UI速度变慢。

选项2:为recyclerView适配器使用类型,并为索引0呈现不同的视图。 答案显示如何执行此操作。

选项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和应用栏”部分)。


我在 Twitter 上看到了这个,我们该怎么做?https://drive.google.com/file/d/0B2JZggeoXKKFdG1ENmZEdWFIa0k/view?usp=sharing - Marcus
我更新了答案。你可以用三种不同的方法实现这个。 - arsent

4

我建议你给滚动视图添加监听器,检测用户是否向下滚动,如果是,则“隐藏”顶部。最简单的方法是使用在Android Studio中创建新活动时默认菜单上提供的默认“滚动活动”,该活动使用Material Design。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接