androidandroid-recyclerviewandroid-flexboxlayout

Horizontal RecyclerView with dynamic item height


I am trying to create a horizontal recycler view with dynamic height by following this stack overflow post. The solution seems working. But the recycler view items disappear when I try to remove an item from recycler view.

recyclerview layout:

    <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:clipToPadding="false"
    android:orientation="horizontal"
    android:paddingStart="13dp"
    android:paddingEnd="13dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Recycler view item layout:

<androidx.cardview.widget.CardView
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="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp"
app:cardBackgroundColor="#BCAAAA"
app:cardElevation="2dp">

    <TextView
        android:padding="36dp"
        android:id="@+id/textViewName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="22sp"
        tools:text="Test String"/>

</androidx.cardview.widget.CardView>

Activity:

     val flexBoxLayoutManager = FlexboxLayoutManager(this, FlexDirection.ROW, FlexWrap.NOWRAP)
    with(recyclerView) {
        layoutManager = flexBoxLayoutManager
        adapter = RecyclerViewAdapter()
        setHasFixedSize(false)
    }

Adapter:

 class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    var textViewName: TextView = itemView.findViewById(R.id.textViewName)
    init {
        updateLayoutParamsToAllowHorizontalScrolling()
    }
    private fun updateLayoutParamsToAllowHorizontalScrolling() {
        (itemView.layoutParams as? FlexboxLayoutManager.LayoutParams)?.let {
            it.flexShrink = 0.0f
            it.alignSelf = AlignItems.FLEX_START
        }
    }
}

    override fun onBindViewHolder(holder: RecyclerViewAdapter.ViewHolder, position: Int) {
    val item = listItems[position]
    var msg = "Sample item no:$position "
    for(i in 0..position){
        msg += "dynamic content \n"
    }
    holder.textViewName.text = msg
    holder.itemView.setOnClickListener {
        val pos = listItems.indexOf(item)
        listItems.removeAt(pos)
        notifyItemRemoved(pos)
    }
}

A screen recording of the output:

enter image description here

Is there any way to fix this issue? Or Are there any other approach to implement horizontal recyclerveiw with dynamic height?


Solution

  • I have fixed the issue without using FlexBoxLayoutManger.

    Used LinearLayoutManager with horizontal orientation and the following code in onBindViewHolder in RecyclerViewAdapter.

      holder.itemView.post {
            val wMeasureSpec =
                View.MeasureSpec.makeMeasureSpec(holder.itemView.width, View.MeasureSpec.EXACTLY)
            val hMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
            holder.itemView.measure(wMeasureSpec, hMeasureSpec)
            if (holder.itemView.measuredHeight > holder.itemView.height) {
                holder.itemView.layoutParams =
                    (holder.itemView.layoutParams as ViewGroup.LayoutParams)
                        .apply {
                                height = holder.itemView.measuredHeight
                        }
            }
        }