ScrollView/NestedScrollView中的RecyclerView无法正确滚动 [英] RecyclerView inside a ScrollView/NestedScrollView does not scroll properly
问题描述
我有一个布局,该布局具有一个 CardView
和一个与其关联的 FloatingActionButton
.在 CardView
(这是 RecyclerView
)下方有一个答复列表.有时 CardViews'
的高度大于屏幕的高度,所以我对 CardView
使用了 layout_height ="wrap_content"
并包装了整个 ScrollView
中的> LinearLayout .
但是,这会导致在滚动 RecyclerView
的项目时出现问题(因为它是 ScrollView
内部的滚动视图).正如某些
在布局中有多个滚动视图(例如RecyclerView + ScrollView)时,以及在recyclerView中滚动时,recyclerView会与父级Scrollview一起滚动.这会在RecyclerView中引起抖动.您可以通过以下方法避免这种抖动.
您可以添加
android:nestedScrollingEnabled ="false"
到 XML 或
recyclerView.setNestedScrollingEnabled(false);
到 Java 中的RecyclerView.
I have a layout which has a CardView
and a FloatingActionButton
associated with it. There is a list of replies below the CardView
(which is a RecyclerView
). Sometimes the CardViews'
height is greater than the screen, so I have used layout_height="wrap_content"
for the CardView
and wrapped the whole LinearLayout
inside a ScrollView
.
However, this causes a problem(since it is a scrolling view inside a ScrollView
) while scrolling the items of the RecyclerView
. As suggested in some of the questions and answers posted, I have used both the NestedScrollView
and the android:nestedScrollingEnabled="true"
tag but the scrolling in the RecyclerView
is still bad.
Here is my Layout
file -
<?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>
Here are some images -
When you have multiple scrolling Views in your layout (eg. RecyclerView + ScrollView) and when you scroll while in your recyclerView, the recyclerView scrolls with the parent Scrollview. this causes jitters in RecyclerView. You can avoid this jitter by the following.
You can add android:nestedScrollingEnabled="false"
to your RecyclerView in XML or
recyclerView.setNestedScrollingEnabled(false);
to your RecyclerView in Java.
这篇关于ScrollView/NestedScrollView中的RecyclerView无法正确滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!