gpt4 book ai didi

ConstraintLayout中的Android RecyclerView不滚动

转载 作者:IT老高 更新时间:2023-10-28 23:29:41 27 4
gpt4 key购买 nike

我在约束布局中有一个 recyclerView,我不能让它滚动,列表只是在屏幕下方继续,没有滚动的可能性。如果我将布局转换为相对布局,则滚动效果很好。

我怎样才能让它滚动?

以下 XML 显示了我的布局,回收站 View 位于底部。布局在屏幕顶部有图像和描述。此屏幕设置占屏幕的 30%。后跟一个分隔符和回收器 View ,它应该占据屏幕的其余部分并且不能滚动

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gradient_top">

<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageViewLock"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toTopOf="@+id/textViewPermissionsTitle"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_phone_lock"/>

<TextView
android:id="@+id/textViewPermissionsTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:gravity="center"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="@string/allow_permissions"
android:textColor="@color/white"
android:textSize="@dimen/description_text_size"
app:layout_constraintBottom_toTopOf="@+id/guideline1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

<android.support.constraint.Guideline
android:id="@+id/guideline1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3"/>


<View
android:id="@+id/viewSeparator"
android:layout_width="match_parent"
android:layout_height="0.7dp"
android:layout_marginTop="10dp"
android:background="@color/bright_blue"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline1"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewPermissions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarSize="1dp"
android:scrollbarThumbVertical="@color/white"
android:scrollbars="vertical"
app:layout_constraintTop_toBottomOf="@+id/viewSeparator" />


</android.support.constraint.ConstraintLayout>

最佳答案

RecyclerView 要滚动,必须满足以下两项之一:

  • RecyclerView 的高度小于其所有项目的高度
  • RecyclerView 位于滚动父级中

ConstraintLayout 不是滚动父级,因此我们必须确保 RecyclerView “太小”,这将导致它让用户滚动其子级.

最简单的方法是给 RecyclerView 一个定义的高度,像这样:

android:layout_height="200dp"

当然,这只有在您提前知道您希望 RecyclerView 到底有多大时才有效,而且通常情况并非如此。

更好的解决方案是使用约束来定义 RecyclerView 的高度。第一步是给它一个高度0dp,可以认为是“匹配约束”。换句话说,您是在告诉系统使 RecyclerView 尽可能高,以满足其顶部和底部约束。

接下来,您必须定义顶部和底部约束。最简单的方法是将 RecyclerView 的顶部约束到父级的顶部,并将其底部约束到父级的底部。可能看起来像这样:

android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

或者,您可以相对于其他一些 View 定位 RecyclerView。可能看起来像这样:

android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/viewAboveMe"
app:layout_constraintBottom_toTopOf="@+id/viewBelowMe"

只要您将 0dp 的高度与顶部和底部约束结合起来(并且只要您的 RecyclerView 实际上小于其内容),这将允许您的 RecyclerView 可以根据需要滚动。

原创

您的 RecyclerView 使用 wrap_content 作为其高度。如果你想让它滚动,你必须提供一个固定的高度,或者,在 ConstraintLayout 中,使用 0dp 作为它的高度并给它一个 app:layout_constraintBottom_xxx 属性。

关于ConstraintLayout中的Android RecyclerView不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45397199/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com