Skip to content

Commit 17f5002

Browse files
committed
修改 SwipeDragHelper 为链式;增加设置 SwipeBackground 能力
1 parent a90f1b9 commit 17f5002

File tree

2 files changed

+88
-26
lines changed

2 files changed

+88
-26
lines changed

app/src/main/java/me/yifeiyuan/flapdev/testcases/SwipeAndDragTestcase.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.yifeiyuan.flapdev.testcases
22

3+
import android.graphics.Color
4+
import android.graphics.drawable.ColorDrawable
35
import android.util.Log
46
import android.view.View
57
import me.yifeiyuan.flap.FlapAdapter
@@ -18,21 +20,19 @@ class SwipeAndDragTestcase : BaseTestcaseFragment() {
1820
override fun onInit(view: View) {
1921
super.onInit(view)
2022

21-
SwipeDragHelper(adapter).apply {
22-
isDragEnable = true
23-
isSwipeEnable = true
24-
// swipeFlags = ItemTouchHelper.START
25-
26-
doOnMove { fromPosition, toPosition ->
27-
toast("移动交换了 $fromPosition to $toPosition")
28-
}
29-
30-
doOnDismiss {
31-
toast("滑动删除了一个 item , position=$it")
32-
}
33-
34-
attachToRecyclerView(recyclerView)
35-
}
23+
val swipeDragHelper = SwipeDragHelper(adapter)
24+
.withDragEnable(true)
25+
.withSwipeEnable(true)
26+
// .withDragFlags()
27+
// .withSwipeFlags()
28+
.withSwipeBackground(ColorDrawable(Color.parseColor("#ff0000")))
29+
.doOnItemDismiss {
30+
toast("滑动删除了一个 item , position=$it")
31+
}
32+
.doOnItemMove { fromPosition, toPosition ->
33+
toast("移动交换了 $fromPosition to $toPosition")
34+
}
35+
.attachToRecyclerView(recyclerView)
3636

3737
recyclerView.addItemDecoration(spaceItemDecoration)
3838
}

flap/src/main/java/me/yifeiyuan/flap/ext/SwipeDragHelper.kt

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package me.yifeiyuan.flap.ext
22

33
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
57
import androidx.recyclerview.widget.*
6-
import me.yifeiyuan.flap.FlapDebug
78

89
/**
910
*
@@ -18,22 +19,24 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback
1819
/**
1920
* 拖动是否可用
2021
*/
21-
var isDragEnable = true
22+
private var isDragEnable = true
2223

2324
/**
2425
* 滑动删除是否可用
2526
*/
26-
var isSwipeEnable = true
27+
private var isSwipeEnable = true
2728

28-
var dragFlags = -1
29-
var swipeFlags = -1
29+
private var dragFlags = -1
30+
private var swipeFlags = -1
3031

31-
var swipeThreshold = 0.5f
32-
var dragThreshold = 0.5f
32+
private var swipeThreshold = 0.5f
33+
private var dragThreshold = 0.5f
3334

3435
private var onMove: ((fromPosition: Int, toPosition: Int) -> Unit)? = null
3536
private var onDismiss: ((position: Int) -> Unit)? = null
3637

38+
private var swipeBackground: Drawable? = null
39+
3740
private val itemTouchHelper: ItemTouchHelper = ItemTouchHelper(this)
3841

3942
override fun isLongPressDragEnabled(): Boolean {
@@ -92,8 +95,9 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback
9295
return false
9396
}
9497

95-
fun doOnMove(block: (fromPosition: Int, toPosition: Int) -> Unit) {
98+
fun doOnItemMove(block: (fromPosition: Int, toPosition: Int) -> Unit): SwipeDragHelper {
9699
onMove = block
100+
return this
97101
}
98102

99103
//Item 被滑动删除了调用
@@ -102,12 +106,14 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback
102106
callback.onItemDismiss(viewHolder.adapterPosition)
103107
}
104108

105-
fun doOnDismiss(block: (position: Int) -> Unit) {
109+
fun doOnItemDismiss(block: (position: Int) -> Unit): SwipeDragHelper {
106110
onDismiss = block
111+
return this
107112
}
108113

109-
fun attachToRecyclerView(recyclerView: RecyclerView) {
114+
fun attachToRecyclerView(recyclerView: RecyclerView): SwipeDragHelper {
110115
itemTouchHelper.attachToRecyclerView(recyclerView)
116+
return this
111117
}
112118

113119
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
@@ -140,12 +146,68 @@ class SwipeDragHelper(private val callback: Callback) : ItemTouchHelper.Callback
140146

141147
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
142148
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+
}
143164
}
144165

145166
override fun onChildDrawOver(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder?, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
146167
super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
147168
}
148169

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+
149211
interface Callback {
150212
fun onItemDismiss(position: Int)
151213
fun onItemMoved(fromPosition: Int, toPosition: Int)

0 commit comments

Comments
 (0)