android - 具有动态项目高度的水平 RecyclerView

标签 android android-recyclerview android-flexboxlayout

我正在尝试按照此 stack overflow post 创建具有动态高度的水平回收器 View 。该解决方案似乎有效。但是当我尝试从回收器 View 中删除项目时,回收器 View 项目消失了。

回收器 View 布局:

    <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" />

回收站查看项目布局:

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

适配器:

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

输出的屏幕录制:

enter image description here

有什么办法可以解决这个问题吗?或者还有其他方法来实现具有动态高度的水平回收吗?

最佳答案

我已在不使用 FlexBoxLayoutManger 的情况下解决了该问题。

使用具有水平方向的LinearLayoutManager,并在RecyclerViewAdapter的onBindViewHolder中使用以下代码。

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

关于android - 具有动态项目高度的水平 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70895965/

相关文章:

android - 使用 Android 设备确定人的坠落

android - 将 ADB 设置为 TCP/IP 模式永远不会结束

android - 当 Bottom Sheet android中的recyclerview时防止用户在 Bottom Sheet 中拖动

android - FlexboxLayout - 在第二行android的末尾显示标签或芯片或标签计数

android - 无法使用 ANDROID PREVIEW SYSTEM IMAGE 刷入 Nexus 7[2012][Wifi]

android 同步 sqlite 与 Parse.com

android - RecyclerView 项目在 RecyclerView 中变得不可见,滚动时有多种项目类型

android - 获取 RecyclerView 中的可见项目

android - Flexboxlayout 项目之间的间距以编程方式