如何使用回收器视图和页面上的其他项目滚动页面



在xml布局中,我有一个回收器视图。 CardView 位于 RecyclerView 的顶部。 那么如何同时滚动这两个项目。

使用此 recyclerView.setNestedScrollingEnabled(false(

您可以将 cardview、recyclerview 和其他视图放在 NestedScrollView 中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:nestedScrollingEnabled="false"
android:layout_weight="1"/>

</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>

希望这会有所帮助!!

嵌套滚动视图中添加卡片视图和回收器视图,如果要以父级身份滚动,请设置nestedScrollingEnabled=false。所以,你的xml看起来像下面

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="false" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>

希望对您有所帮助!

谢谢。

最新更新