RecyclerView flows over the screen

断了今生、忘了曾经 提交于 2020-06-17 09:37:48

问题


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.

However, if I have multiple elements that need to be over the top of RecyclerView, the RecyclerView cannot be fit into the screen.

Code for the first scenario:

<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:

Code for the second scenario:

<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.


回答1:


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" />

to

<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"/>

You should also NOT use match_parent in children of ConstraintLayout.



来源:https://stackoverflow.com/questions/61859576/recyclerview-flows-over-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!