0

我正在尝试通过遵循此堆栈溢出帖子来创建具有动态高度的水平回收器视图。该解决方案似乎有效。但是,当我尝试从回收站视图中删除项目时,回收站视图项目会消失。

回收站视图布局:

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

活动:

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

输出的屏幕记录:

在此处输入图像描述

有没有办法解决这个问题?或者有没有其他方法来实现动态高度的水平回收?

4

1 回答 1

0

我已经解决了这个问题,但没有使用FlexBoxLayoutManger.

LinearLayoutManager与onBindViewHolderhorizontal中的方向和以下代码一起使用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
                    }
        }
    }
于 2022-02-09T16:07:10.160 回答