1
1
package me.yifeiyuan.flap.ext
2
2
3
3
import android.graphics.Canvas
4
- import android.util.Log
4
+ import android.graphics.Color
5
+ import android.graphics.drawable.ColorDrawable
6
+ import android.graphics.drawable.Drawable
5
7
import androidx.recyclerview.widget.*
6
- import me.yifeiyuan.flap.FlapDebug
7
8
8
9
/* *
9
10
*
@@ -18,22 +19,24 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback
18
19
/* *
19
20
* 拖动是否可用
20
21
*/
21
- var isDragEnable = true
22
+ private var isDragEnable = true
22
23
23
24
/* *
24
25
* 滑动删除是否可用
25
26
*/
26
- var isSwipeEnable = true
27
+ private var isSwipeEnable = true
27
28
28
- var dragFlags = - 1
29
- var swipeFlags = - 1
29
+ private var dragFlags = - 1
30
+ private var swipeFlags = - 1
30
31
31
- var swipeThreshold = 0.5f
32
- var dragThreshold = 0.5f
32
+ private var swipeThreshold = 0.5f
33
+ private var dragThreshold = 0.5f
33
34
34
35
private var onMove: ((fromPosition: Int , toPosition: Int ) -> Unit )? = null
35
36
private var onDismiss: ((position: Int ) -> Unit )? = null
36
37
38
+ private var swipeBackground: Drawable ? = null
39
+
37
40
private val itemTouchHelper: ItemTouchHelper = ItemTouchHelper (this )
38
41
39
42
override fun isLongPressDragEnabled (): Boolean {
@@ -92,8 +95,9 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback
92
95
return false
93
96
}
94
97
95
- fun doOnMove (block : (fromPosition: Int , toPosition: Int ) -> Unit ) {
98
+ fun doOnItemMove (block : (fromPosition: Int , toPosition: Int ) -> Unit ): SwipeDragHelper {
96
99
onMove = block
100
+ return this
97
101
}
98
102
99
103
// Item 被滑动删除了调用
@@ -102,12 +106,14 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback
102
106
callback.onItemDismiss(viewHolder.adapterPosition)
103
107
}
104
108
105
- fun doOnDismiss (block : (position: Int ) -> Unit ) {
109
+ fun doOnItemDismiss (block : (position: Int ) -> Unit ): SwipeDragHelper {
106
110
onDismiss = block
111
+ return this
107
112
}
108
113
109
- fun attachToRecyclerView (recyclerView : RecyclerView ) {
114
+ fun attachToRecyclerView (recyclerView : RecyclerView ): SwipeDragHelper {
110
115
itemTouchHelper.attachToRecyclerView(recyclerView)
116
+ return this
111
117
}
112
118
113
119
override fun onSelectedChanged (viewHolder : RecyclerView .ViewHolder ? , actionState : Int ) {
@@ -140,12 +146,68 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback
140
146
141
147
override fun onChildDraw (c : Canvas , recyclerView : RecyclerView , viewHolder : RecyclerView .ViewHolder , dX : Float , dY : Float , actionState : Int , isCurrentlyActive : Boolean ) {
142
148
super .onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
149
+ if (actionState == ItemTouchHelper .ACTION_STATE_SWIPE ) {
150
+ // 滑动的时候可以绘制背景
151
+ swipeBackground?.let {
152
+ val itemView = viewHolder.itemView
153
+ if (dX > 0 ) {
154
+ it.setBounds(itemView.left, itemView.top, itemView.left + dX.toInt(), itemView.bottom)
155
+ it.draw(c)
156
+ } else {
157
+ it.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
158
+ it.draw(c)
159
+ }
160
+ }
161
+ } else if (actionState == ItemTouchHelper .ACTION_STATE_DRAG ) {
162
+ // 拖动
163
+ }
143
164
}
144
165
145
166
override fun onChildDrawOver (c : Canvas , recyclerView : RecyclerView , viewHolder : RecyclerView .ViewHolder ? , dX : Float , dY : Float , actionState : Int , isCurrentlyActive : Boolean ) {
146
167
super .onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
147
168
}
148
169
170
+ /* *
171
+ * 拖动是否可用
172
+ */
173
+ fun withDragEnable (enable : Boolean ): SwipeDragHelper {
174
+ isDragEnable = enable
175
+ return this
176
+ }
177
+
178
+ /* *
179
+ * 滑动删除是否可用
180
+ */
181
+ fun withSwipeEnable (enable : Boolean ): SwipeDragHelper {
182
+ isSwipeEnable = enable
183
+ return this
184
+ }
185
+
186
+ fun withDragFlags (dragFlags : Int ): SwipeDragHelper {
187
+ this .dragFlags = dragFlags
188
+ return this
189
+ }
190
+
191
+ fun withSwipeFlags (swipeFlags : Int ): SwipeDragHelper {
192
+ this .swipeFlags = swipeFlags
193
+ return this
194
+ }
195
+
196
+ fun withSwipeThreshold (swipeThreshold : Float ): SwipeDragHelper {
197
+ this .swipeThreshold = swipeThreshold
198
+ return this
199
+ }
200
+
201
+ fun withDragThreshold (dragThreshold : Float ): SwipeDragHelper {
202
+ this .dragThreshold = dragThreshold
203
+ return this
204
+ }
205
+
206
+ fun withSwipeBackground (swipeBackground : Drawable ): SwipeDragHelper {
207
+ this .swipeBackground = swipeBackground
208
+ return this
209
+ }
210
+
149
211
interface Callback {
150
212
fun onItemDismiss (position : Int )
151
213
fun onItemMoved (fromPosition : Int , toPosition : Int )
0 commit comments