-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3666041
commit b19edd7
Showing
4 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
SwipeToDelete/src/main/java/com/kedia/swipetodelete/SwipeToPerform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.kedia.swipetodelete | ||
|
||
import android.graphics.Canvas | ||
import androidx.annotation.ColorInt | ||
import androidx.recyclerview.widget.ItemTouchHelper | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.kedia.swipetodelete.utils.DIRECTION | ||
import java.lang.Exception | ||
|
||
fun RecyclerView.addSwipeToPerform( | ||
list: List<DIRECTION>? = emptyList(), | ||
listener: OnSwipeToPerform? = null, | ||
@ColorInt colorOneInt: Int? = null, | ||
@ColorInt colorTwoInt: Int? = null | ||
) { | ||
|
||
var swipeDirs = ItemTouchHelper.RIGHT | ||
list?.apply { | ||
for (element in list) { | ||
if (element != DIRECTION.RIGHT) { | ||
swipeDirs = swipeDirs or DIRECTION.valueOf(element.name).direction | ||
} | ||
} | ||
} | ||
|
||
val simpleCallback = object : ItemTouchHelper.SimpleCallback(0 , swipeDirs) { | ||
override fun onMove( | ||
recyclerView: RecyclerView, | ||
viewHolder: RecyclerView.ViewHolder, | ||
target: RecyclerView.ViewHolder | ||
): Boolean { | ||
return false | ||
} | ||
|
||
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { | ||
listener?.swipeToPerform(adapterPosition = viewHolder.adapterPosition) | ||
} | ||
|
||
override fun onChildDraw( | ||
c: Canvas, | ||
recyclerView: RecyclerView, | ||
viewHolder: RecyclerView.ViewHolder, | ||
dX: Float, | ||
dY: Float, | ||
actionState: Int, | ||
isCurrentlyActive: Boolean | ||
) { | ||
c.clipRect(0f, viewHolder.itemView.top.toFloat(), | ||
dX, viewHolder.itemView.bottom.toFloat()) | ||
|
||
if (colorTwoInt != null && colorOneInt == null) | ||
throw Exception("Color One cannot be null if Color Two is non null") | ||
|
||
if (colorTwoInt == null) { | ||
colorOneInt?.apply { c.drawColor(this) } | ||
} else { | ||
if(dX < width / 2) | ||
colorOneInt?.apply { c.drawColor(this) } | ||
else | ||
colorTwoInt?.apply { c.drawColor(this) } | ||
} | ||
|
||
super.onChildDraw( | ||
c, | ||
recyclerView, | ||
viewHolder, | ||
dX, | ||
dY, | ||
actionState, | ||
isCurrentlyActive | ||
) | ||
} | ||
} | ||
ItemTouchHelper(simpleCallback).attachToRecyclerView(this) | ||
} | ||
|
||
interface OnSwipeToPerform { | ||
fun swipeToPerform(adapterPosition: Int) | ||
} |
10 changes: 10 additions & 0 deletions
10
SwipeToDelete/src/main/java/com/kedia/swipetodelete/utils/utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.kedia.swipetodelete.utils | ||
|
||
import androidx.recyclerview.widget.ItemTouchHelper | ||
|
||
enum class DIRECTION(val direction: Int) { | ||
UP (ItemTouchHelper.UP), | ||
DOWN(ItemTouchHelper.DOWN), | ||
RIGHT(ItemTouchHelper.RIGHT), | ||
LEFT(ItemTouchHelper.LEFT) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters