RecyclerView在屏幕上流动 [英] RecyclerView flows over the screen
问题描述
我有一个替换FrameLayout的片段.在此片段中,如果只有RecyclerView,则可以将其高度设置为match_parent,一切都可以正常工作.
I have a fragment that replaces FrameLayout. In this fragment, if I had only RecyclerView, I could set its height to match_parent and everything could work well.
但是,如果我有多个元素需要位于RecyclerView的顶部,则RecyclerView无法容纳在屏幕中.
However, if I have multiple elements that need to be over the top of RecyclerView, the RecyclerView cannot be fit into the screen.
第一种情况的代码:
<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:background="@color/mid_white">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_leaderboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
应用第一种情况时的片段截图:
Screenshot of fragment when the first scenario is applied:
第二种情况的代码:
<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:background="@color/mid_white">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_leaderboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider"
/>
<TextView
android:id="@+id/rankText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Rank"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/google_green"
android:background="@color/transparent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="4dp"
android:paddingBottom="4dp"
android:layout_marginLeft="16dp"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/usernameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Username"
android:textSize="18sp"
android:paddingBottom="4dp"
android:textStyle="bold"
android:textColor="@color/google_green"
android:background="@color/transparent"
android:layout_marginLeft="50dp"
app:layout_constraintStart_toStartOf="@id/rankText"
app:layout_constraintTop_toTopOf="@id/rankText" />
<TextView
android:id="@+id/balanceText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Balance"
android:textSize="18sp"
android:paddingBottom="4dp"
android:textStyle="bold"
android:textColor="@color/google_green"
android:background="@color/transparent"
android:layout_marginRight="26dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/rankText" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="4dp"
app:layout_constraintTop_toBottomOf="@id/rankText"
android:background="@color/google_green" />
应用第二种情况时的片段截图:
Screenshot of fragment when the second scenario is applied:
在第二种情况下,我无法再进行滑动,因此看不到最后一个元素.
In the second scenario, I can't swipe more, so I cannot see the last element.
推荐答案
您必须限制RecyclerView的底部并将高度设置为匹配约束".改变这个
You have to constrain your RecyclerView's bottom and set the height to "match constraint". Change this
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_leaderboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider" />
到
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_leaderboard"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider"/>
您也不应在ConstraintLayout的子级中使用match_parent
.
You should also NOT use match_parent
in children of ConstraintLayout.
这篇关于RecyclerView在屏幕上流动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!