【问题标题】:Android RecyclerView and ListView - Parent view not scrolling enough, ListView hiddenAndroid RecyclerView 和 ListView - 父视图滚动不足,ListView 隐藏
【发布时间】:2020-05-09 05:56:14
【问题描述】:

我有一个应用程序,其中在同一布局中有一个 RecyclerView 和一个 ListView。这些组件单独运行良好。 RecyclerView 和 ListViews 是完全可见和可滚动的。但是,当我将它们放在一个布局父级上时:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingBottom="30dp"
    android:background="@color/Cerulean">


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.02" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.12" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.05" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.30" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:divider="@color/Cerulean"
        android:dividerHeight="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline17" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:divider="@color/Cerulean"
        android:dividerHeight="5dp"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerView" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitCenter"
        android:src="@drawable/logo"
        app:layout_constraintBottom_toTopOf="@+id/guideline17"
        app:layout_constraintEnd_toStartOf="@+id/guideline14"
        app:layout_constraintStart_toStartOf="@+id/guideline13"
        app:layout_constraintTop_toTopOf="@+id/guideline15" />

</androidx.constraintlayout.widget.ConstraintLayout>

我只有一堆指导方针来帮助我放置徽标,然后我有我的 recyclerView 和 listViews。

我以前只有 ListView,我可以滚动浏览所有内容。然后我添加了我的 RecyclerView,它也运行良好。

但是,当 RecyclerView 膨胀并显示其所有内容时,我无法再滚动到我的 ListView。我可以看到我的 RecyclerView 中的所有项目,但是当我滚动浏览我的 RecyclerView 时,我点击了我的活动的“底部”,并且我不再能够看到我的 ListView。

我还尝试添加一个将nestedScrollingEnabled 设置为true 的ScrollView,但这没有帮助。我只能查看我的 RecyclerView 和我的 ListView 的第一行,更不用说滚动体验变得迟钝和低于标准。

我该怎么做才能保证我可以滚动浏览我的 RecyclerView 和 ListView?

【问题讨论】:

    标签: android listview android-recyclerview


    【解决方案1】:

    不可能通过一个接一个地“连接”RecyclerViewListView 中的项目(即,一旦您滚动浏览一个项目,它就会滚动到另一个项目)。您应该将两者合并为一个包含所有项目的 RecyclerView

    试图让父级将一个滚动到另一个只会导致奇怪的滚动行为(正如您在 cmets 在另一个答案中所指出的那样)。

    【讨论】:

    • 我不想连接 RecyclerView 和 ListView,我只是想让我的视图平滑滚动。我会考虑将两者合并到一个 RecyclerView 中,看看会发生什么。
    • “连接”我的意思是,一旦你滚动浏览一个,它就会滚动到另一个 - 这不是你想要做的吗?我在答案中澄清了这一点。
    • 感谢您的澄清。是的,这就是我想要做的。我将尝试将它们组合在一个 RecyclerView 中。
    【解决方案2】:

    试试这个:

        <ScrollView 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="fill_parent"
     android:layout_height="wrap_content"
     android:fillViewport="true">
    
    android.support.constraint.ConstraintLayout
    android:id="@+id/task_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/first_list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/second_list_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />
    
    <ListView
        android:id="@+id/second_list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/first_list_view" />
    

    【讨论】:

    • 这不是有效的 XML。
    • 我刚试了一下,还是不行。我可以滚动浏览我的 RecyclerView,但我只能看到我的 listView 的第一行。卷轴也很滞后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    相关资源
    最近更新 更多