在 ScrollView 中使用 ListView 滚动整个视图 [英] Scrolling the whole view with a ListView insida a ScrollView

查看:34
本文介绍了在 ScrollView 中使用 ListView 滚动整个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making an app and I can't find any solutions.Here is my code:

<?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,但这对我也不起作用.

What I'm trying to achive is add CardViews inside my ListView and be able to scroll not just the ListView but the whole view. I tried RecyclerView before but that didn't work for me either.

这是我的想法:

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

and Scroll the whole thing not just the ListView.

推荐答案

需要使用 NestedScrollView.

You need to use NestedScrollView.

NestedScrollView 就像 ScrollView,但它支持在新旧版本的 Android 上同时充当嵌套滚动父项和子项.默认情况下启用嵌套滚动.

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

请参考 NestedScrollView 文档

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

Don't forget to add recyclerView.setNestedScrollingEnabled(false); to your RecyclerView.

这篇关于在 ScrollView 中使用 ListView 滚动整个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆