-
Notifications
You must be signed in to change notification settings - Fork 51
/
SwipeRemoveItemActivity.kt
103 lines (87 loc) · 3.46 KB
/
SwipeRemoveItemActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package smartrecycleradapter.feature
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_simple_item.recyclerView
import smartadapter.SmartRecyclerAdapter
import smartadapter.extension.SmartViewHolderBinder
import smartadapter.viewevent.listener.OnClickEventListener
import smartadapter.viewevent.model.ViewEvent
import smartadapter.viewevent.swipe.BasicSwipeEventBinder
import smartadapter.viewevent.swipe.SwipeFlags
import smartrecycleradapter.R
import smartrecycleradapter.feature.simpleitem.SimpleItemViewHolder
import smartrecycleradapter.utils.showToast
/*
* Created by Manne Öhlund on 2019-08-11.
* Copyright (c) All rights reserved.
*/
class SwipeRemoveItemActivity : BaseSampleActivity() {
private lateinit var smartRecyclerAdapter: SmartRecyclerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.title = "Swipe remove item"
val items = (0..50).toMutableList()
smartRecyclerAdapter = SmartRecyclerAdapter
.items(items)
.map(Integer::class, SimpleItemViewHolder::class)
.add(OnClickEventListener {
showToast("onClick ${it.position}")
})
.add(SwipeRemoveItemBinder(ItemTouchHelper.LEFT) {
// Remove item from SmartRecyclerAdapter data set
smartRecyclerAdapter.removeItem(it.viewHolder.adapterPosition)
})
.into(recyclerView)
}
class SwipeRemoveItemBinder(
override var swipeFlags: SwipeFlags,
override var eventListener: (ViewEvent.OnItemSwiped) -> Unit
) : BasicSwipeEventBinder(
eventListener = eventListener
), SmartViewHolderBinder {
override fun onChildDraw(
canvas: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
val itemView = viewHolder.itemView
val icon = ContextCompat.getDrawable(
viewHolder.itemView.context,
R.drawable.ic_delete_black_24dp
)
val background = ColorDrawable(Color.RED)
val iconMargin = (itemView.height - icon!!.intrinsicHeight) / 2
val iconTop = itemView.top + (itemView.height - icon.intrinsicHeight) / 2
val iconBottom = iconTop + icon.intrinsicHeight
val iconLeft = itemView.right - iconMargin - icon.intrinsicWidth
val iconRight = itemView.right - iconMargin
icon.setBounds(
iconLeft,
iconTop,
iconRight,
iconBottom
)
background.setBounds(
(itemView.right + dX).toInt(),
itemView.top,
itemView.right,
itemView.bottom
)
if (dX.toInt() == 0) { // view is unSwiped
background.setBounds(0, 0, 0, 0)
}
background.draw(canvas)
if (-dX > (icon.intrinsicWidth + iconMargin)) // Draw icon only on full visibility
icon.draw(canvas)
}
}
}