activity kotlin RecyclerView 左滑显示删除按钮 删除item 完整代码实现
时间: 2023-06-17 22:02:45 浏览: 176
以下是基于 Kotlin 和 RecyclerView 实现左滑显示删除按钮和删除 item 的完整代码实现:
首先,我们需要在 app 的 build.gradle 文件中添加 RecyclerView 和 Material Design 的依赖:
```groovy
dependencies {
implementation "com.google.android.material:material:1.4.0"
implementation "androidx.recyclerview:recyclerview:1.2.1"
}
```
接着,在 activity 的布局文件中添加 RecyclerView:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager" />
```
然后,我们需要定义 RecyclerView 的 Adapter 和 ViewHolder:
```kotlin
class MyAdapter(private val items: List<String>, private val listener: (Int) -> Unit) :
RecyclerView.Adapter<MyAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_layout, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(items[position], listener)
}
override fun getItemCount(): Int {
return items.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val textView: TextView = itemView.findViewById(R.id.textView)
private val deleteButton: ImageButton = itemView.findViewById(R.id.deleteButton)
fun bind(item: String, listener: (Int) -> Unit) {
textView.text = item
deleteButton.setOnClickListener { listener(adapterPosition) }
}
}
}
```
其中,ViewHolder 中有一个 deleteButton,我们将在左滑时显示它。
接下来,我们需要在 activity 中设置 RecyclerView 的 Adapter 和 ItemTouchHelper,实现左滑显示删除按钮和删除 item 的功能:
```kotlin
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var adapter: MyAdapter
private lateinit var items: MutableList<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
items = mutableListOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5")
adapter = MyAdapter(items) { position ->
items.removeAt(position)
adapter.notifyItemRemoved(position)
}
recyclerView = findViewById(R.id.recyclerView)
recyclerView.adapter = adapter
val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(
0,
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
) {
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
if (direction == ItemTouchHelper.LEFT) {
items.removeAt(position)
adapter.notifyItemRemoved(position)
} else {
// do something on right swipe
}
}
override fun onChildDraw(
c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
val itemView = viewHolder.itemView
val deleteIcon = ContextCompat.getDrawable(
this@MainActivity,
R.drawable.ic_delete
)
val iconMargin = resources.getDimension(R.dimen.icon_margin).toInt()
val iconTop = itemView.top + (itemView.height - deleteIcon!!.intrinsicHeight) / 2
val iconBottom = iconTop + deleteIcon.intrinsicHeight
if (dX > 0) {
val iconLeft = itemView.left + iconMargin
val iconRight = itemView.left + iconMargin + deleteIcon.intrinsicWidth
deleteIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
c.clipRect(itemView.left, itemView.top, itemView.left + dX.toInt(), itemView.bottom)
deleteIcon.draw(c)
} else {
val iconLeft = itemView.right - iconMargin - deleteIcon.intrinsicWidth
val iconRight = itemView.right - iconMargin
deleteIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
c.clipRect(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
deleteIcon.draw(c)
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
})
itemTouchHelper.attachToRecyclerView(recyclerView)
}
}
```
在 ItemTouchHelper 的 onChildDraw 方法中,我们根据滑动的方向和距离画出了删除按钮的图标,并通过 clipRect 方法限制了按钮的显示范围。
最后,我们在 res/drawable 目录下创建了一个名为 ic_delete 的图标,用于显示删除按钮。
完整代码实现请参考以下 GitHub 仓库:https://github.com/linroid/RecyclerViewSwipeToDelete
阅读全文