Skip to content

Commit 3e7f52d

Browse files
committed
added color options
1 parent 594c927 commit 3e7f52d

3 files changed

Lines changed: 57 additions & 5 deletions

File tree

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package com.kedia.swipetodelete
22

3-
import android.R
4-
import android.graphics.*
5-
import android.view.View
3+
import android.graphics.Canvas
4+
import android.graphics.Color
5+
import androidx.annotation.ColorInt
66
import androidx.recyclerview.widget.ItemTouchHelper
77
import androidx.recyclerview.widget.ItemTouchHelper.RIGHT
88
import androidx.recyclerview.widget.RecyclerView
9+
import java.lang.Exception
910

1011

1112
object SwipeToDelete {
1213

1314
fun RecyclerView.addSwipeToDelete(
1415
list: List<DIRECTION> = emptyList(),
15-
listener: OnSwiped? = null
16+
listener: OnSwiped? = null,
17+
@ColorInt colorOneInt: Int? = null,
18+
@ColorInt colorTwoInt: Int? = null
1619
) {
1720

1821
var swipeDirs = RIGHT
@@ -35,10 +38,49 @@ object SwipeToDelete {
3538
listener?.swipeToDelete(adapterPosition = viewHolder.adapterPosition)
3639
this@addSwipeToDelete.adapter?.notifyItemRemoved(viewHolder.adapterPosition)
3740
}
41+
42+
override fun onChildDraw(
43+
c: Canvas,
44+
recyclerView: RecyclerView,
45+
viewHolder: RecyclerView.ViewHolder,
46+
dX: Float,
47+
dY: Float,
48+
actionState: Int,
49+
isCurrentlyActive: Boolean
50+
) {
51+
c.clipRect(0f, viewHolder.itemView.top.toFloat(),
52+
dX, viewHolder.itemView.bottom.toFloat())
53+
54+
if (colorTwoInt != null && colorOneInt == null)
55+
throw Exception("Color One cannot be null if Color Two is non null")
56+
57+
if (colorTwoInt == null) {
58+
colorOneInt?.apply { c.drawColor(this) }
59+
} else {
60+
if(dX < width / 2)
61+
colorOneInt?.apply { c.drawColor(this) }
62+
else
63+
colorTwoInt?.apply { c.drawColor(this) }
64+
}
65+
66+
super.onChildDraw(
67+
c,
68+
recyclerView,
69+
viewHolder,
70+
dX,
71+
dY,
72+
actionState,
73+
isCurrentlyActive
74+
)
75+
}
3876
}
3977
ItemTouchHelper(simpleCallback).attachToRecyclerView(this)
4078
}
4179

80+
private fun Float.isPositive(): Boolean {
81+
return this > 0
82+
}
83+
4284
interface OnSwiped {
4385
fun swipeToDelete(adapterPosition: Int)
4486
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape android:shape="rectangle"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<stroke android:width="2dp"
6+
android:color="@color/colorPrimaryDark" />
7+
8+
</shape>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.kedia.customswipelibrary
33
import android.os.Bundle
44
import android.util.Log
55
import androidx.appcompat.app.AppCompatActivity
6+
import androidx.core.content.ContextCompat
7+
import androidx.core.content.res.ResourcesCompat
68
import androidx.recyclerview.widget.LinearLayoutManager
79
import com.kedia.swipetodelete.SwipeToDelete
810
import com.kedia.swipetodelete.SwipeToDelete.addSwipeToDelete
@@ -28,7 +30,7 @@ class MainActivity : AppCompatActivity(), SwipeToDelete.OnSwiped {
2830

2931
val list = listOf(SwipeToDelete.DIRECTION.LEFT,
3032
SwipeToDelete.DIRECTION.RIGHT)
31-
recycler.addSwipeToDelete(list, this)
33+
recycler.addSwipeToDelete(list, this, ContextCompat.getColor(this, R.color.colorAccent), ContextCompat.getColor(this, R.color.colorPrimaryDark))
3234
}
3335

3436
override fun swipeToDelete(adapterPosition: Int) {

0 commit comments

Comments
 (0)