如何使用wrap_content动态设置Recycler视图高度?
我有一个包含回收器视图的片段,该视图是使用从服务器获取的数据填充的。该片段位于活动内的 FragmentContainerView
内。我希望 recyclerView 的高度是动态的。本质上使用 wrap_content
作为高度参数,但是当我这样做时,recyclerView 的高度为 0 并且根本不显示。
为什么 wrap_content
不动态调整我的 fragmentContainerView
高度?
片段
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/calendarRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
活动
<?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=".activities.DashboardActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="com.yambo.yates.fragments.WorkoutCalendarFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="@layout/workout_calendar_fragment" />
</androidx.constraintlayout.widget.ConstraintLayout>
I have a fragment containing a recycler view which is populated using data fetched from a server. This fragment is inside a FragmentContainerView
within the activity. I want the height of the recyclerView to be dynamic. Essentially using wrap_content
as the height parameter, but when I do that the recyclerView has a height of 0 and doesn't show up at all.
Why doesn't wrap_content
dynamically adjust the height for my fragmentContainerView
height?
Fragment
<?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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/calendarRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity
<?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=".activities.DashboardActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="com.yambo.yates.fragments.WorkoutCalendarFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="@layout/workout_calendar_fragment" />
</androidx.constraintlayout.widget.ConstraintLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RecyclerViews 已经根据内容动态调整高度。因此,只要您的 XML 至少是wrap_content,它就应该显示所有内容。您确定您的数据有效吗?
另外,如果您的片段仅包含 RecyclerView,您可以删除父 ConstraintLayout 并将 XML 更改为:
RecyclerViews already dynamically adjust height based on contents. So as long as your XML is at least wrap_content it should display everything. Are you sure your data is valid?
Also, if your fragment only contains the RecyclerView you can remove the parent ConstraintLayout and change your XML to: