ConstraintLayout 中的 Android RecyclerView 不滚动 [英] Android RecyclerView in ConstraintLayout doesn't scroll

查看:33
本文介绍了ConstraintLayout 中的 Android RecyclerView 不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a recyclerView inside a constraint layout and I cannot make it scroll, the list just continues below the screen without scroll possibility. If I turn the layout into relative layout the scroll works fine.

我怎样才能让它滚动?

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

the the following XML shows my layout, the recycler view is in the bottom. the layout has an image and description at the top of the screen. this screen setup takes 30% of the screen. followed by and a separator and the recycler view that should take the rest of the screen and that cannot scroll

<?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 滚动,必须满足以下两点之一:

For a RecyclerView to scroll, one of two things must be true:

  • RecyclerView 的高度小于其所有项目
  • RecyclerView 位于滚动父级内
  • The RecyclerView has a smaller height than all of its items
  • The RecyclerView is inside a scrolling parent

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

ConstraintLayout is not a scrolling parent, so we have to make sure that the RecyclerView is "too small", which will cause it to let the user scroll its children.

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

The easiest way is to just give the RecyclerView a defined height, with something like this:

android:layout_height="200dp"

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

Of course, this only works if you know ahead of time exactly how big you want your RecyclerView to be, and this is not usually the case.

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

A better solution is to use constraints to define the height of your RecyclerView. The first step is to give it a height of 0dp, which can be thought of as "match constraints". In other words, you're telling the system to make the RecyclerView as tall as it needs to be in order to satisfy its top and bottom constrains.

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

Next, you must define top and bottom constraints. The simplest would be to constrain the RecyclerView's top to the parent's top and its bottom to the parent's bottom. That might look like this:

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

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

Or, you could position the RecyclerView relative to some other views. That might look like this:

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

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

As long as you combine a height of 0dp with both top and bottom constraints (and as long as your RecyclerView is actually smaller than its contents), this will allow your RecyclerView to scroll as desired.

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

Your RecyclerView is using wrap_content for its height. If you want it to scroll, you must provide a fixed height, or, inside a ConstraintLayout, use 0dp for its height and give it an app:layout_constraintBottom_xxx attribute.

这篇关于ConstraintLayout 中的 Android RecyclerView 不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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