我有一个布局,其中包含一个
CardView
和一个与之关联的 FloatingActionButton
。在 CardView
下方有一系列回复(即 RecyclerView
)。有时候 CardView
的高度大于屏幕,因此我使用了 layout_height="wrap_content"
来设置 CardView
的高度,并将整个 LinearLayout
包装在一个 ScrollView
中。
然而,这会引起一个问题(因为它是一个嵌套在 ScrollView
中的滚动视图),当滚动 RecyclerView
的项目时。根据一些问题和回答的建议,我已经使用了 NestedScrollView
和 android: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>
以下是一些图片 -
ViewCompat.setNestedScrollingEnabled(mRecyclerView, false);
。 @yadav_vi - marios-codes