kotlinandroid-recyclerviewsectionedrecyclerviewadapter

RecyclerView using SectionedRecyclerViewAdapter only shows first header


I'm using SectionedRecyclerViewAdapter from luizgrp/SectionedRecyclerViewAdapter as adapter for my RecyclerView. But only the first header is shown. No content is shown and neither is the second header. Any clue about what I'm doing wrong?

My RoundSection.kt looks as below

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

My fragment_workout_details.xml looks as below

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))
    }
}

My fragment_workout_details.xml looks as below

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

My round_header.xml looks as below

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

My round_exercise_item.xml looks as below

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

Solution

  • Setting android:layout_height="match_parent" on both round_header.xml and round_exercise_item.xml solved the problem.