Skip to content

Commit

Permalink
added methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pknotfound committed Oct 24, 2020
1 parent 08b0cca commit 7538e1a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object DragAndDrop: View.OnTouchListener, GestureDetector.OnGestureListener {

private lateinit var holder: RecyclerView.ViewHolder

fun RecyclerView.addDragToSwipe(): ItemTouchHelper {
fun RecyclerView.addDragToSwipe(listener: onDragged? = null): ItemTouchHelper {

val itemTouchCallback = object : ItemTouchHelper.Callback() {
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
Expand Down Expand Up @@ -52,6 +52,7 @@ object DragAndDrop: View.OnTouchListener, GestureDetector.OnGestureListener {
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
listener?.onPositionDragged(viewHolder.adapterPosition, target.adapterPosition)
adapter?.notifyItemMoved(viewHolder.adapterPosition, target.adapterPosition)
return true
}
Expand Down Expand Up @@ -111,4 +112,8 @@ object DragAndDrop: View.OnTouchListener, GestureDetector.OnGestureListener {
return false
}

interface onDragged {
fun onPositionDragged(positionStart: Int, positionEnd: Int)
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.kedia.swipetodelete

import android.util.Log
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
listener: OnSwiped? = null,
@ColorInt colorOneInt: Int? = null,
@ColorInt colorTwoInt: Int? = null
) {

var swipeDirs = RIGHT
Expand All @@ -33,10 +39,48 @@ object SwipeToDelete {
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())

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)
}

private fun Float.isPositive(): Boolean {
return this > 0
}

interface OnSwiped {
fun swipeToDelete(adapterPosition: Int)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kedia.customswipelibrary
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.kedia.swipetodelete.DragAndDrop
import com.kedia.swipetodelete.DragAndDrop.addDragToSwipe
Expand Down Expand Up @@ -32,7 +33,7 @@ class MainActivity : AppCompatActivity(), SwipeToDelete.OnSwiped {

val list = listOf(SwipeToDelete.DIRECTION.LEFT,
SwipeToDelete.DIRECTION.RIGHT)
recycler.addSwipeToDelete(list, this)
recycler.addSwipeToDelete(list, this, ContextCompat.getColor(this, R.color.colorPrimaryDark))
// try {
// SwipeToDelete.javaClass.getDeclaredMethod("some").invoke(SwipeToDelete)
// } catch (e: NoSuchMethodException) {
Expand Down

0 comments on commit 7538e1a

Please sign in to comment.