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

Android RecyclerView点击按钮动态更新列表项且保留滚动位置的实现方法

解决方案

你要的效果完全可以实现,有两种可选方案,优先推荐第一种,不需要重新加载列表也不会改变滚动位置:

方案1:动态更新可见列表项展示状态(最优)

不需要刷新整个RecyclerView,切换模式后立刻更新可见项的显示内容,滚动位置完全不变。

  1. 新增展示模式状态标识
    在ViewModel中定义当前展示模式的可观察状态:
enum class DisplayMode {
    DETAIL, // B1模式:展示姓名、年龄、生日
    PROPERTY, // B2模式:展示身高、体重
    COLOR // B3模式:展示头发颜色、眼睛颜色
}

// 使用StateFlow或LiveData都可以,只要能观察状态变化
val currentDisplayMode = MutableStateFlow(DisplayMode.DETAIL)
  1. 改造列表项布局list_item_1
    用DataBinding的可见性控制,把三类要展示的内容都放在同一个布局里,根据当前模式控制显示隐藏:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="android.view.View"/>
        <variable
            name="person"
            type="你的包名路径.Person" />
        <variable
            name="displayMode"
            type="你的包名路径.DisplayMode" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">

        <!-- B1模式展示区域 -->
        <LinearLayout
            android:visibility="@{displayMode == DisplayMode.DETAIL ? View.VISIBLE : View.GONE}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`姓名:` + person.name}"/>
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`年龄:` + person.age}"/>
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`生日:` + person.dob}"/>
        </LinearLayout>

        <!-- B2模式展示区域 -->
        <LinearLayout
            android:visibility="@{displayMode == DisplayMode.PROPERTY ? View.VISIBLE : View.GONE}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`身高:` + person.height}"/>
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`体重:` + person.weight}"/>
        </LinearLayout>

        <!-- B3模式展示区域 -->
        <LinearLayout
            android:visibility="@{displayMode == DisplayMode.COLOR ? View.VISIBLE : View.GONE}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`头发色值:` + person.hairColor}"/>
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{`眼睛色值:` + person.eyeColor}"/>
        </LinearLayout>
    </LinearLayout>
</layout>
  1. 切换模式时仅更新可见列表项
    在Adapter中新增模式更新方法,不需要调用全量刷新:
fun switchDisplayMode(newMode: DisplayMode) {
    currentDisplayMode = newMode
    // 只遍历当前屏幕可见的ViewHolder,逐个更新展示状态
    for (i in 0 until recyclerView.childCount) {
        val childView = recyclerView.getChildAt(i)
        val holder = recyclerView.getChildViewHolder(childView) as PersonViewHolder
        holder.binding.displayMode = newMode
        holder.binding.executePendingBindings()
    }
}

不在屏幕内的列表项在后续滚动时会自动按照新的模式绑定数据,完全不需要重新加载整个列表。

方案2:全量刷新后保留滚动位置

如果你一定要重新加载整个列表,可以在刷新前后记录和恢复滚动位置:

val layoutManager = dataListView.layoutManager as GridLayoutManager
// 刷新前记录第一个完全可见的item位置和偏移量
val firstVisiblePos = layoutManager.findFirstCompletelyVisibleItemPosition()
val topOffset = dataListView.getChildAt(0)?.top ?: 0

// 执行列表刷新操作
adapter.notifyDataSetChanged()

// 刷新完成后恢复滚动位置
layoutManager.scrollToPositionWithOffset(firstVisiblePos, topOffset)

你提到的动态设置/重置listitem取值完全可行,上述方案1就是直接修改ViewHolder的绑定变量,即可实时更新列表项的展示内容,不需要重新创建列表项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 04:36:06