ScrollView/NestedScrollView中的RecyclerView无法正确滚动

yad*_*_vi 4 android scrollview android-recyclerview

我的布局有一个CardView和一个FloatingActionButton相关的布局.下面有一个回复列表CardView(这是一个RecyclerView).有时CardViews'高度大于屏幕,所以我已经习惯layout_height="wrap_content"CardView将整个LinearLayout内部包裹起来ScrollView.

但是,这会导致问题(因为它是一个内部的滚动视图ScrollView),同时滚动项目RecyclerView.正如在发布的一些问题答案中所建议的那样,我已经使用了NestedScrollViewandroid:nestedScrollingEnabled="true"标签,但滚动RecyclerView仍然很糟糕.

这是我的Layout档案 -

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.forum.reply.ReplyActivity">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/reply_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:titleTextColor="@android:color/white"/>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:nestedScrollingEnabled="true"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:orientation="vertical">

                <android.support.v7.widget.CardView
                    android:id="@+id/topic_card"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="@dimen/card_margin"
                    android:paddingLeft="@dimen/card_margin"
                    android:paddingRight="@dimen/card_margin"
                    android:paddingTop="@dimen/card_margin">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:paddingEnd="@dimen/card_margin"
                        android:paddingStart="@dimen/card_margin">

                        <android.support.v7.widget.AppCompatTextView
                            android:id="@+id/topic_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="8dp"
                            android:layout_marginTop="8dp"
                            android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

                        <android.support.v7.widget.AppCompatTextView
                            android:id="@+id/topic_content"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
                    </LinearLayout>
                </android.support.v7.widget.CardView>

                <ProgressBar
                    android:id="@+id/reply_progressbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:indeterminate="true"
                    android:visibility="visible"/>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/list_of_replies"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="invisible"/>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/reply_to_topic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_reply_white_24dp"
        app:layout_anchor="@id/topic_card"
        app:layout_anchorGravity="bottom|right|end"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

这是一些图像 -

带有FloatingActionButton的CardView可见

从CardView滚动到RecyclerView是顺利的

向下滚动并不顺利

向上滚动也不顺利

Sid*_*ghe 14

如果布局中有多个滚动视图(例如RecyclerView + ScrollView),并且在recyclelerView中滚动时,recyclelerView将使用父Scrollview滚动.这会导致RecyclerView中的抖动.您可以通过以下方式避免此抖动.

您可以
android:nestedScrollingEnabled="false" 使用XML添加到RecyclerView 或
recyclerView.setNestedScrollingEnabled(false); 使用Java添加 到RecyclerView .

  • 请注意,如果您想支持 v21 之前的设备,您必须使用 `ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);` @yadav_vi (6认同)
  • 您的布局中有多个滚动视图,因此当您在触摸 recyclerView 时滚动时,recyclerView 会随父 Scrollview 一起滚动。这会导致 RecyclerView 出现抖动,因此使用nestedScrollingEnabled =“false”可以停止recyclerView的滚动,并且触发的唯一滚动来自父ScrollView。我还使用 .setNestScrollingEnabled() 以编程方式执行此操作,并且在早于 API v21 的设备上工作正常。 (2认同)

mar*_*des 6

如果您想支持早于api 21的设备,那么您应该使用

ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);

在你的活动/片段中