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

Android & Kotlin:如何用Custom RecyclerView实现带相邻链接的步骤列表?

可以用RecyclerView实现这个需求,具体操作步骤如下

1. 定义步骤数据模型

先创建一个数据类,用来存储每个步骤的核心信息,包括状态标识:

data class Step(
    val id: Int,
    val title: String,
    val content: String,
    val isCompleted: Boolean, // 是否已完成
    val isCurrent: Boolean    // 是否为当前选中步骤
)

2. 自定义Item布局

每个列表项需要包含步骤指示器、内容区域,以及连接相邻项的竖线,示例布局item_step.xml:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingVertical="16dp">

    <!-- 步骤指示器 -->
    <TextView
        android:id="@+id/tv_step_number"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:gravity="center"
        android:textColor="@color/white"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <!-- 步骤内容区 -->
    <LinearLayout
        android:id="@+id/ll_step_content"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:orientation="vertical"
        app:layout_constraintLeft_toRightOf="@id/tv_step_number"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tv_step_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_step_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:layout_marginTop="4dp"/>
    </LinearLayout>

    <!-- 连接上下步骤的竖线 -->
    <View
        android:id="@+id/v_line"
        android:layout_width="2dp"
        android:layout_height="0dp"
        android:background="@color/grey_400"
        app:layout_constraintLeft_toLeftOf="@id/tv_step_number"
        app:layout_constraintTop_toBottomOf="@id/tv_step_number"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

同时给步骤指示器准备3种状态的背景drawable:完成态(绿色填充)、当前态(蓝色填充)、未完成态(灰色边框)。

3. 实现RecyclerView.Adapter

在Adapter里处理每个item的状态渲染、竖线显示逻辑,以及点击跳转事件:

class StepAdapter(
    private val stepList: List<Step>,
    private val onStepClick: (Int) -> Unit
) : RecyclerView.Adapter<StepAdapter.StepViewHolder>() {

    inner class StepViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvStepNumber: TextView = itemView.findViewById(R.id.tv_step_number)
        val tvStepTitle: TextView = itemView.findViewById(R.id.tv_step_title)
        val tvStepContent: TextView = itemView.findViewById(R.id.tv_step_content)
        val vLine: View = itemView.findViewById(R.id.v_line)

        fun bind(step: Step) {
            tvStepNumber.text = step.id.toString()
            tvStepTitle.text = step.title
            tvStepContent.text = step.content

            // 设置指示器样式
            tvStepNumber.background = when {
                step.isCurrent -> itemView.context.getDrawable(R.drawable.bg_step_current)
                step.isCompleted -> itemView.context.getDrawable(R.drawable.bg_step_completed)
                else -> itemView.context.getDrawable(R.drawable.bg_step_uncompleted)
            }

            // 最后一个item隐藏竖线
            vLine.visibility = if (adapterPosition == stepList.lastIndex) View.GONE else View.VISIBLE

            // 绑定点击事件
            itemView.setOnClickListener { onStepClick(adapterPosition) }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StepViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_step, parent, false)
        return StepViewHolder(view)
    }

    override fun onBindViewHolder(holder: StepViewHolder, position: Int) {
        holder.bind(stepList[position])
    }

    override fun getItemCount() = stepList.size
}

4. 配置RecyclerView并处理跳转逻辑

在Activity/Fragment中初始化RecyclerView,设置布局管理器,同时实现相邻步骤的跳转:

class StepListActivity : AppCompatActivity() {
    private lateinit var rvSteps: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_step_list)

        rvSteps = findViewById(R.id.rv_steps)
        rvSteps.layoutManager = LinearLayoutManager(this)

        // 模拟已下载的步骤数据
        val stepList = listOf(
            Step(1, "账户验证", "完成邮箱绑定", true, false),
            Step(2, "基础设置", "配置个人信息", true, true),
            Step(3, "权限配置", "设置数据访问权限", false, false),
            Step(4, "完成初始化", "启动服务", false, false)
        )

        val adapter = StepAdapter(stepList) { position ->
            // 点击item跳转至相邻步骤,这里示例为跳转到下一个,可根据需求改为上一个或指定步骤
            if (position < stepList.lastIndex) {
                rvSteps.smoothScrollToPosition(position + 1)
                // 如需切换当前选中状态,可更新stepList数据后调用notifyItemChanged
            }
        }
        rvSteps.adapter = adapter
    }
}

5. 细节优化

  • 用smoothScrollToPosition替代scrollToPosition,实现平滑滚动效果
  • 若步骤数量较多,可开启RecyclerView的预加载优化
  • 更新步骤状态时,使用DiffUtil进行局部刷新,提升性能

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 23:01:12