You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GridLayoutManager下RecyclerView列表项展开后无法收缩的问题求助

GridLayoutManager下RecyclerView列表项无法收缩的问题

问题现象

使用GridLayoutManager(列数为2)的RecyclerView,每个列表项包含按钮和默认隐藏的内容:

  • 首次点击按钮展开当前项时,同行的另一项会被一同撑开
  • 再次点击隐藏内容后,所在行无法收缩回原高度,但切换为LinearLayoutManager或列数设为1时功能正常

相关代码

列表项布局(item_test.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/box"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_margin="8dp"
    android:layout_weight="1"
    android:background="#00ff00"
    android:orientation="vertical"
    android:padding="8dp">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Press me"
        android:visibility="visible" />

    <View
        android:id="@+id/hidden_view"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#0000ff"
        android:visibility="gone" />

</LinearLayout>

Adapter代码

class TestAdapter : RecyclerView.Adapter<ViewHolder>() {

    val items = arrayOf(false, false, false, false)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemTestBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun getItemCount(): Int {
        return items.size
    }

     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.itemBinding.hiddenView.isVisible = items[position]

        holder.itemBinding.button.setOnClickListener {
            items[position] = !items[position]
            notifyItemChanged(position)
        }

        return
    }
}

open class ViewHolder internal constructor(val itemBinding: ItemTestBinding) :
    RecyclerView.ViewHolder(itemBinding.root)

主布局(activity_main.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:background="#ff0000"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
        app:layout_constraintTop_toTopOf="parent"
        app:spanCount="2"
        tools:itemCount="4"
        tools:listitem="@layout/item_test" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#ffff00"
        app:layout_constraintTop_toBottomOf="@id/recycler"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity代码

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.recycler.adapter = TestAdapter()
    }
}

解决方案

问题根源是GridLayoutManager会缓存行的高度,当单个列表项收缩后,管理器不会主动重新计算整行高度。可以通过以下方式修复:

方案1:刷新同行所有列表项

在按钮点击事件中,除了刷新当前项,还要刷新同行的另一项,强制GridLayoutManager重新计算行高:

holder.itemBinding.button.setOnClickListener {
    items[position] = !items[position]
    // 计算同行的另一个item位置(适用于spanCount=2的情况)
    val peerPosition = if (position % 2 == 0) position + 1 else position - 1
    if (peerPosition >= 0 && peerPosition < itemCount) {
        notifyItemChanged(peerPosition)
    }
    notifyItemChanged(position)
}

方案2:配置GridLayoutManager强制回收子View

在MainActivity中给GridLayoutManager设置recycleChildrenOnDetach为true,让View被回收时重新测量:

val layoutManager = GridLayoutManager(this, 2)
layoutManager.recycleChildrenOnDetach = true
binding.recycler.layoutManager = layoutManager
binding.recycler.adapter = TestAdapter()

这两种方案都能让行在列表项收缩后恢复到原高度,其中方案1更精准,只刷新必要的项;方案2配置简单,适合所有动态高度的Grid场景。


内容的提问来源于stack exchange,提问作者P1x

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 22:20:56