【问题标题】:Scrolling the whole view with a ListView insida a ScrollView在 ScrollView 中使用 ListView 滚动整个视图
【发布时间】:2021-10-28 04:25:49
【问题描述】:

我正在制作应用程序,但找不到任何解决方案。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
    android:background="@color/DarkGray"
    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">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/BackGroundColor"
                android:orientation="horizontal"
                android:paddingTop="4dp"
                android:paddingBottom="4dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:textAllCaps="true"
                    android:textColor="@color/SecondaryTextColor"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAlignment="center"
                    android:textAllCaps="true"
                    android:textColor="@color/SecondaryTextColor"
                    android:textSize="18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="@color/BlueGray"
                android:nestedScrollingEnabled="false"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/BackGroundColor"
                android:padding="4dp"
                android:textAlignment="center"
                android:textColor="@color/PrimaryTextColor"
                android:textSize="22sp" />

            <ListView
                android:id="@+id/eventsListView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="2dp"/>

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

我想要实现的是在我的 ListView 中添加 CardViews,并且不仅可以滚动 ListView,还可以滚动整个视图。我之前尝试过 RecyclerView,但这对我也不起作用。

这是我的想法:

并滚动整个内容,而不仅仅是 ListView。

【问题讨论】:

  • 第 2 课:不要将 ListView 放在 ScrollView 中。
  • 如果不是我可以使用的 RecyclerView 或 ListView,我应该使用什么来存储多个用户添加的 CardView?
  • 你应该使用 NestedScrollView。

标签: java android android-studio listview scrollview


【解决方案1】:

你需要使用 NestedScrollView。

NestedScrollView 与 ScrollView 类似,但它支持在新旧版本的 Android 上同时充当嵌套滚动父项和子项。默认启用嵌套滚动。

请参考NestedScrollView documentation

不要忘记将recyclerView.setNestedScrollingEnabled(false); 添加到您的 RecyclerView。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2015-05-22
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    相关资源
    最近更新 更多