ConstraintLayout中的Android RecyclerView不滚动 [英] Android RecyclerView in ConstraintLayout doesn't scroll
问题描述
我在约束布局中有一个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屋!