使用 SectionedRecyclerViewAdapter 的 RecyclerView 只显示第一个 header

RecyclerView using SectionedRecyclerViewAdapter only shows first header

我正在使用 luizgrp/SectionedRecyclerViewAdapter 的 SectionedRecyclerViewAdapter 作为我的 RecyclerView 的适配器。但只显示第一个 header。没有显示任何内容,第二个 header 也没有显示。关于我做错了什么的任何线索?

我的 RoundSection.kt 如下所示

class RoundSection(private val title: String, private val items: List<Pair<RoundExercise, Exercise>>, sectionParameters: SectionParameters) : Section(sectionParameters) {
    override fun getContentItemsTotal(): Int = items.size

    override fun getItemViewHolder(view: View): RecyclerView.ViewHolder = ItemViewHolder(view)

    override fun onBindItemViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        val itemViewHolder = holder as ItemViewHolder
        itemViewHolder.exerciseName.text = items[position].second.name
    }

    override fun getHeaderViewHolder(view: View): RecyclerView.ViewHolder = HeaderViewHolder(view)

    override fun onBindHeaderViewHolder(holder: RecyclerView.ViewHolder) {
        val headerViewHolder = holder as HeaderViewHolder
        headerViewHolder.roundHeader.text = title
    }

    internal class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val exerciseName: TextView = itemView.exerciseName
    }

    internal class HeaderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val roundHeader: TextView = itemView.roundHeader
    }
}

我的 fragment_workout_details.xml 如下所示

class WorkoutDetailsFragment : Fragment(R.layout.fragment_workout_details) {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val sectionParameters = SectionParameters.builder()
                .itemResourceId(R.layout.round_exercise_item)
                .headerResourceId(R.layout.round_header)
                .build()

        val sectionAdapter = SectionedRecyclerViewAdapter()
        roundRecyclerView.layoutManager = LinearLayoutManager(context)
        roundRecyclerView.adapter = sectionAdapter

        val exercise = Exercise("exercise name", "exercise desc", 0, 1)
        val roundExercise = RoundExercise(0, 1, 5, 0, 0, 0)
        sectionAdapter.addSection(RoundSection("Round 1", listOf(roundExercise to exercise), sectionParameters))

        val exercise1 = Exercise("exercise name1", "exercise desc1", 0, 2)
        val roundExercise1 = RoundExercise(0, 2, 5, 0, 0, 0)
        sectionAdapter.addSection(RoundSection("Round 2", listOf(roundExercise1 to exercise1), sectionParameters))
    }
}

我的 fragment_workout_details.xml 如下所示

<?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"
    android:padding="16dp"
    tools:context=".ui.WorkoutDetailsFragment">

    <TextView
        android:id="@+id/workoutName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/workout_name_workout_details_fragment"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    <TextView
        android:id="@+id/workoutDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/workout_description_workout_details_fragment"
        app:layout_constraintTop_toBottomOf="@+id/workoutName" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/roundRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/workoutDescription" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的 round_header.xml 如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">

    <TextView
        android:id="@+id/roundHeader"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:text="Row Header Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

</RelativeLayout>

我的 round_exercise_item.xml 如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">

    <TextView
        android:id="@+id/exerciseName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:text="Exercise Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

</RelativeLayout>

round_header.xmlround_exercise_item.xml 上设置 android:layout_height="match_parent" 解决了问题。