限制 RecyclerView 的最大尺寸
我将一个屏幕放在一起,其中3个控件垂直堆叠。具体而言,夹在两个TextView之间的回收模型。我有两种情况要解决。
- 当循环系统中显示很少的项目时,应将3个项目包装在屏幕顶部,下面有空白。
- 当循环系统中显示许多项目时,全屏幕将与屏幕底部的最后一个文本视图一起使用,并在中心循环中滚动。
我努力将上一个文本视图的位置正确正确,因为如果回收库中有很多项目,则可以将其从屏幕上推开,或者埋在回收界后面。我需要的一种方法是告诉回收即包装内容,但是在开始将文本视图从屏幕上推开时停止增长。
以下是我的布局,有什么建议吗?
<?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">
<TextView
android:id="@+id/topItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/middleItem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/middleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="@+id/bottomItem"
app:layout_constraintTop_toBottomOf="@+id/topItem" />
<TextView
android:id="@+id/bottomItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/middleItem" />
</androidx.constraintlayout.widget.ConstraintLayout>
I'm putting together a screen with 3 controls stacked vertically. Specifically, a RecyclerView sandwiched between two TextView. I have two situations I am trying to address.
- When few items are displayed in the RecyclerView the 3 items should be packed towards the top of the screen with blank space BELOW them.
- When many items are displayed in the RecyclerView the full screen will be used with the last TextView along the bottom of the screen and scrolling in the center RecyclerView.
I'm struggling to get the position of the last TextView correct as it either gets pushed off the screen or buried behind the RecyclerView if there are many items in the RecyclerView. What I need is a way to tell the RecyclerView to wrap contents, but stop growing when it starts to push the TextView off of the screen.
Below is my layout, any suggestions?
<?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">
<TextView
android:id="@+id/topItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/middleItem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/middleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="@+id/bottomItem"
app:layout_constraintTop_toBottomOf="@+id/topItem" />
<TextView
android:id="@+id/bottomItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/middleItem" />
</androidx.constraintlayout.widget.ConstraintLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试以下操作:
在 RecyclerView 中使用三个项目:
RecyclerView 中有 50 个项目:
主要更改是:
app:layout_constraintVertical_bias="0.0"
添加到顶部 TextView,以将链始终置于顶部;app:layout_constrainedHeight="true"
以确保视图不会离开屏幕;0dp
并为 ConstraintLayout 的直接子级设置水平约束。更新:虽然上面的方法有效,但有点过头了。正如评论中所述,只需将 RecyclerView 的高度设置为
0dp
即可实现相同的效果。Try the following:
With three items in the RecyclerView:
With 50 items in the RecyclerView:
The key changes are:
app:layout_constraintVertical_bias="0.0"
to the top TextView to position the chain always at the top;app:layout_constrainedHeight="true"
to the RecyclerView to make sure views don't go off-screen;0dp
and set the horizontal constraints for direct children of the ConstraintLayout.Update: Although the above works, it is a little overdone. As noted in the comments, the same effect can be accomplished by simply setting the height of the RecyclerView to
0dp
.