Skip to content

Commit b19edd7

Browse files
committed
added swipe to perform
1 parent 3666041 commit b19edd7

4 files changed

Lines changed: 98 additions & 7 deletions

File tree

SwipeToDelete/src/main/java/com/kedia/swipetodelete/SwipeToDelete.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.graphics.Canvas
44
import androidx.annotation.ColorInt
55
import androidx.recyclerview.widget.ItemTouchHelper
66
import androidx.recyclerview.widget.RecyclerView
7+
import com.kedia.swipetodelete.utils.DIRECTION
78
import java.lang.Exception
89

910

@@ -83,9 +84,3 @@ interface OnSwiped {
8384
fun swipeToDelete(adapterPosition: Int)
8485
}
8586

86-
enum class DIRECTION(val direction: Int) {
87-
UP (ItemTouchHelper.UP),
88-
DOWN(ItemTouchHelper.DOWN),
89-
RIGHT(ItemTouchHelper.RIGHT),
90-
LEFT(ItemTouchHelper.LEFT)
91-
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.kedia.swipetodelete
2+
3+
import android.graphics.Canvas
4+
import androidx.annotation.ColorInt
5+
import androidx.recyclerview.widget.ItemTouchHelper
6+
import androidx.recyclerview.widget.RecyclerView
7+
import com.kedia.swipetodelete.utils.DIRECTION
8+
import java.lang.Exception
9+
10+
fun RecyclerView.addSwipeToPerform(
11+
list: List<DIRECTION>? = emptyList(),
12+
listener: OnSwipeToPerform? = null,
13+
@ColorInt colorOneInt: Int? = null,
14+
@ColorInt colorTwoInt: Int? = null
15+
) {
16+
17+
var swipeDirs = ItemTouchHelper.RIGHT
18+
list?.apply {
19+
for (element in list) {
20+
if (element != DIRECTION.RIGHT) {
21+
swipeDirs = swipeDirs or DIRECTION.valueOf(element.name).direction
22+
}
23+
}
24+
}
25+
26+
val simpleCallback = object : ItemTouchHelper.SimpleCallback(0 , swipeDirs) {
27+
override fun onMove(
28+
recyclerView: RecyclerView,
29+
viewHolder: RecyclerView.ViewHolder,
30+
target: RecyclerView.ViewHolder
31+
): Boolean {
32+
return false
33+
}
34+
35+
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
36+
listener?.swipeToPerform(adapterPosition = viewHolder.adapterPosition)
37+
}
38+
39+
override fun onChildDraw(
40+
c: Canvas,
41+
recyclerView: RecyclerView,
42+
viewHolder: RecyclerView.ViewHolder,
43+
dX: Float,
44+
dY: Float,
45+
actionState: Int,
46+
isCurrentlyActive: Boolean
47+
) {
48+
c.clipRect(0f, viewHolder.itemView.top.toFloat(),
49+
dX, viewHolder.itemView.bottom.toFloat())
50+
51+
if (colorTwoInt != null && colorOneInt == null)
52+
throw Exception("Color One cannot be null if Color Two is non null")
53+
54+
if (colorTwoInt == null) {
55+
colorOneInt?.apply { c.drawColor(this) }
56+
} else {
57+
if(dX < width / 2)
58+
colorOneInt?.apply { c.drawColor(this) }
59+
else
60+
colorTwoInt?.apply { c.drawColor(this) }
61+
}
62+
63+
super.onChildDraw(
64+
c,
65+
recyclerView,
66+
viewHolder,
67+
dX,
68+
dY,
69+
actionState,
70+
isCurrentlyActive
71+
)
72+
}
73+
}
74+
ItemTouchHelper(simpleCallback).attachToRecyclerView(this)
75+
}
76+
77+
interface OnSwipeToPerform {
78+
fun swipeToPerform(adapterPosition: Int)
79+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.kedia.swipetodelete.utils
2+
3+
import androidx.recyclerview.widget.ItemTouchHelper
4+
5+
enum class DIRECTION(val direction: Int) {
6+
UP (ItemTouchHelper.UP),
7+
DOWN(ItemTouchHelper.DOWN),
8+
RIGHT(ItemTouchHelper.RIGHT),
9+
LEFT(ItemTouchHelper.LEFT)
10+
}

app/src/main/java/com/kedia/customswipelibrary/MainActivity.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.kedia.customswipelibrary
22

33
import android.os.Bundle
4+
import android.util.Log
45
import androidx.appcompat.app.AppCompatActivity
56
import androidx.core.content.ContextCompat
67
import androidx.recyclerview.widget.LinearLayoutManager
78
import com.kedia.swipetodelete.*
9+
import com.kedia.swipetodelete.utils.DIRECTION
810
import kotlinx.android.synthetic.main.activity_main.*
911

10-
class MainActivity : AppCompatActivity(), OnSwiped, OnDragged {
12+
class MainActivity : AppCompatActivity(), OnSwiped, OnDragged, OnSwipeToPerform {
1113

1214
private val list = mutableListOf<String>()
1315
private lateinit var adapter: Adapter
@@ -30,6 +32,7 @@ class MainActivity : AppCompatActivity(), OnSwiped, OnDragged {
3032
val list = listOf(
3133
DIRECTION.LEFT,
3234
DIRECTION.RIGHT)
35+
recycler.addSwipeToPerform(list, this, ContextCompat.getColor(this, R.color.colorPrimaryDark))
3336
recycler.addSwipeToDelete(list, this, ContextCompat.getColor(this, R.color.colorPrimaryDark))
3437
// try {
3538
// SwipeToDelete.javaClass.getDeclaredMethod("some").invoke(SwipeToDelete)
@@ -46,4 +49,8 @@ class MainActivity : AppCompatActivity(), OnSwiped, OnDragged {
4649
adapter.removeItem(adapterPosition)
4750
}
4851

52+
override fun swipeToPerform(adapterPosition: Int) {
53+
Log.d("TAG!!!!", "swipeToPerform: swiped")
54+
}
55+
4956
}

0 commit comments

Comments
 (0)