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

Android Compose中Lazy List的滑动删除/查看实现

Android 列表项滑动删除基础实现示例

核心思路

基于Android官方提供的ItemTouchHelper组件,配合RecyclerView实现滑动删除功能,这是官方原生且稳定的解决方案。


1. 实现基础RecyclerView适配器

先编写一个简单的列表适配器,用于展示数据并提供删除接口:

class SimpleListAdapter(private val dataList: MutableList<String>) :
    RecyclerView.Adapter<SimpleListAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.item_text)
    }

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textView.text = dataList[position]
    }

    override fun getItemCount() = dataList.size

    // 对外暴露的删除方法,带动画刷新
    fun removeItem(position: Int) {
        dataList.removeAt(position)
        notifyItemRemoved(position)
    }
}

对应的列表项布局list_item_layout.xml(极简示例):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:textSize="18sp"/>

2. 实现ItemTouchHelper回调类

这个类负责监听滑动事件、触发删除逻辑:

class SwipeToDeleteCallback(private val adapter: SimpleListAdapter) :
    ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {

    // 不需要拖拽功能,返回0禁用
    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean {
        return false
    }

    // 滑动结束后执行删除操作
    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        val position = viewHolder.adapterPosition
        adapter.removeItem(position)
    }
}
  • 构造参数传入适配器,直接调用其删除方法
  • ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT配置允许左右两个方向滑动删除

3. 在Activity中关联组件

在Activity/Fragment里完成RecyclerView初始化和滑动功能绑定:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
        // 初始化测试数据
        val dataList = mutableListOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")
        val adapter = SimpleListAdapter(dataList)
        
        recyclerView.adapter = adapter
        recyclerView.layoutManager = LinearLayoutManager(this)

        // 将滑动删除功能绑定到RecyclerView
        val itemTouchHelper = ItemTouchHelper(SwipeToDeleteCallback(adapter))
        itemTouchHelper.attachToRecyclerView(recyclerView)
    }
}

可选自定义:滑动时的视觉反馈

如果需要在滑动时显示删除背景和图标,可以在SwipeToDeleteCallback中重写onChildDraw方法:

override fun onChildDraw(
    c: Canvas,
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder,
    dX: Float,
    dY: Float,
    actionState: Int,
    isCurrentlyActive: Boolean
) {
    val itemView = viewHolder.itemView
    val deleteIcon = ContextCompat.getDrawable(recyclerView.context, R.drawable.ic_delete) ?: return
    val iconMargin = (itemView.height - deleteIcon.intrinsicHeight) / 2
    val iconTop = itemView.top + iconMargin
    val iconBottom = iconTop + deleteIcon.intrinsicHeight

    if (dX > 0) {
        // 向右滑动:绘制红色背景+左侧删除图标
        val background = ColorDrawable(Color.RED)
        background.setBounds(itemView.left, itemView.top, (itemView.left + dX).toInt(), itemView.bottom)
        background.draw(c)

        val iconLeft = itemView.left + iconMargin
        val iconRight = iconLeft + deleteIcon.intrinsicWidth
        deleteIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
        deleteIcon.draw(c)
    } else {
        // 向左滑动:绘制红色背景+右侧删除图标
        val background = ColorDrawable(Color.RED)
        background.setBounds((itemView.right + dX).toInt(), itemView.top, itemView.right, itemView.bottom)
        background.draw(c)

        val iconRight = itemView.right - iconMargin
        val iconLeft = iconRight - deleteIcon.intrinsicWidth
        deleteIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
        deleteIcon.draw(c)
    }
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 12:27:18