-
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
9414238
commit 6d61ab0
Showing
4 changed files
with
163 additions
and
166 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
138 changes: 66 additions & 72 deletions
138
SwipeToDelete/src/main/java/com/kedia/swipetodelete/SwipeToDelete.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 |
---|---|---|
@@ -1,95 +1,89 @@ | ||
package com.kedia.swipetodelete | ||
|
||
import android.graphics.Canvas | ||
import android.graphics.Color | ||
import androidx.annotation.ColorInt | ||
import androidx.recyclerview.widget.ItemTouchHelper | ||
import androidx.recyclerview.widget.ItemTouchHelper.RIGHT | ||
import androidx.recyclerview.widget.RecyclerView | ||
import java.lang.Exception | ||
|
||
|
||
object SwipeToDelete { | ||
fun RecyclerView.addSwipeToDelete( | ||
list: List<DIRECTION> = emptyList(), | ||
listener: OnSwiped? = null, | ||
@ColorInt colorOneInt: Int? = null, | ||
@ColorInt colorTwoInt: Int? = null | ||
) { | ||
|
||
fun RecyclerView.addSwipeToDelete( | ||
list: List<DIRECTION> = emptyList(), | ||
listener: OnSwiped? = null, | ||
@ColorInt colorOneInt: Int? = null, | ||
@ColorInt colorTwoInt: Int? = null | ||
) { | ||
|
||
var swipeDirs = RIGHT | ||
for (element in list) { | ||
if (element != DIRECTION.RIGHT) { | ||
swipeDirs = swipeDirs or DIRECTION.valueOf(element.name).direction | ||
} | ||
var swipeDirs = ItemTouchHelper.RIGHT | ||
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 | ||
} | ||
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?.swipeToDelete(adapterPosition = viewHolder.adapterPosition) | ||
this@addSwipeToDelete.adapter?.notifyItemRemoved(viewHolder.adapterPosition) | ||
} | ||
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { | ||
listener?.swipeToDelete(adapterPosition = viewHolder.adapterPosition) | ||
this@addSwipeToDelete.adapter?.notifyItemRemoved(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()) | ||
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 == null) | ||
throw Exception("Color One cannot be null if Color Two is non null") | ||
|
||
if (colorTwoInt == null) { | ||
if (colorTwoInt == null) { | ||
colorOneInt?.apply { c.drawColor(this) } | ||
} else { | ||
if(dX < width / 2) | ||
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 | ||
) | ||
else | ||
colorTwoInt?.apply { c.drawColor(this) } | ||
} | ||
} | ||
ItemTouchHelper(simpleCallback).attachToRecyclerView(this) | ||
} | ||
|
||
private fun Float.isPositive(): Boolean { | ||
return this > 0 | ||
super.onChildDraw( | ||
c, | ||
recyclerView, | ||
viewHolder, | ||
dX, | ||
dY, | ||
actionState, | ||
isCurrentlyActive | ||
) | ||
} | ||
} | ||
ItemTouchHelper(simpleCallback).attachToRecyclerView(this) | ||
} | ||
|
||
interface OnSwiped { | ||
fun swipeToDelete(adapterPosition: Int) | ||
} | ||
private fun Float.isPositive(): Boolean { | ||
return this > 0 | ||
} | ||
|
||
enum class DIRECTION(val direction: Int) { | ||
UP (ItemTouchHelper.UP), | ||
DOWN(ItemTouchHelper.DOWN), | ||
RIGHT(ItemTouchHelper.RIGHT), | ||
LEFT(ItemTouchHelper.LEFT) | ||
} | ||
interface OnSwiped { | ||
fun swipeToDelete(adapterPosition: Int) | ||
} | ||
|
||
} | ||
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
Oops, something went wrong.