RecyclerView和NestedScrollView只占屏幕的一半



我有一个布局,屏幕底部有一个EditTextButton。我希望NestedScrollViewRecyclerView占据2个元素上方的空间。然而,他们只拿走了其中的一部分

这是我的代码

<?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"
tools:context=".HomeActivity">
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/etMessage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".HomeActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvMessages"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:nestedScrollingEnabled="true" />
</androidx.core.widget.NestedScrollView>
<EditText
android:id="@+id/etMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter your message"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnSend"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/etMessage" />
</androidx.constraintlayout.widget.ConstraintLayout>

在NestedScrollView 中添加此行

android:fillViewport="true"

如果RecyclerView中有必要数量的项目,它将具有与NestedScrollView相同的高度。您可以计算必要的项目高度。我也不认为你的布局中需要NestedScrollView,因为RecyclerView可以在没有它的情况下滚动。

最新更新