diff --git a/README.md b/README.md index 509f9f4..0167a20 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,63 @@ # SwipeLibrary -Extension Library to add swipe functionality to the RecyclerView +SwipeLibrary provides extension functions for handling swipe actions in RecyclerView. + +You can add **Swipe To Delete** and **Drag and Shift** like functionalities with just one line of code. + + +# Extension Functions + +SwipeLibrary contains the following functions: +- [Swipe To Delete](#swipe-to-delete) +- [Drag to Shift](#drag-to-shift) + +## Swipe To Delete + +Add swipe to delete RecyclerView's items from the list, which a single line of code. + +Use *addSwipeToDelete* as RecyclerView's extension function, to add this functionality to your RecyclerView. +This extension function has **two** optional parameter. +> The first parameter is the list of directions in which swipe should be allowed. The directions can be either **TOP**, **BOTTOM**, **RIGHT**, **LEFT**. +The list of directions can be made like this, + `val list = listOf(SwipeToDelete.DIRECTION.LEFT,SwipeToDelete.DIRECTION.RIGHT)` +If no value is passed for the list, by default, **RIGHT** is the direction for swipe. + +> The second parameter is the *listener* for the interface method. The by default value of this parameter is null. If passed, the activity/fragment should implement `SwipeToDelete.OnSwiped`. This method will be called when the RecyclerView item is swiped out, and it returns the position of the element that was swiped. + +**Implementation of interface method** +In a notes app, if you wish to delete the note from your database, after it is swiped, then you can used this method to perform the action. The note can be deleted by getting the *NOTE* using the adapter position inside the list passed to RecyclerView Adapter. + + override fun swipeToDelete(adapterPosition: Int) { + adapter.removeItem(adapterPosition) + } + +> The third parameter is an optional parameter, for the Color integer, if the user wants a color in the background when the view is swiped out. This is how the user can pass the Color int as the parameter, `ContextCompat.getColor(this, R.color.colorAccent)` + +[![rec-2020-10-24_14.38.15.gif](https://s8.gifyu.com/images/rec-2020-10-24_14.38.15.gif)](https://gifyu.com/image/8FMg) + + +> The fourth parameter is an optional parameter, for the second Color integer, if the user wants two colors in the background, the user can pass both **Parameter 3 and Parameter 4**. + +[![rec-2020-10-24_14.43.59.gif](https://s8.gifyu.com/images/rec-2020-10-24_14.43.59.gif)](https://gifyu.com/image/8Fpk) + + +--- +*** +___ + +## Drag To Shift + +Highlight, move and shift items of RecyclerView with just a single line of code. +Use *addDragToSwipe* as RecyclerView's extension function, to add this functionality to your RecyclerView. This extension has **one** optional parameter. + +> The parameter is the listener for the interface method. The by default value for this parameter is null. If passed, the activity/fragment should implement `DragAndDrop.onDragged`. This method will be called when the RecyclerView item is dragged and dropped to some other position, and it returns the two positions that were swapped. + +**Implementation of interface method** :- +In a notes app, if you wish to change positions of two notes in your database, after they are swapped, then you can used this method to perform the action. The notes can be swapped by getting the _NOTES_ using the adapter positions inside the list passed to RecyclerView Adapter. + + override fun onPositionDragged(positionStart: Int, positionEnd: Int) { + adapter.moveItem(positionStart, positionEnd) + } + +[![rec-2020-10-24_00.10.0459f8d3ce489a4f0f.gif](https://s8.gifyu.com/images/rec-2020-10-24_00.10.0459f8d3ce489a4f0f.gif)](https://gifyu.com/image/8jl5) + diff --git a/SwipeToDelete/src/main/java/com/kedia/swipetodelete/DragAndDrop.kt b/SwipeToDelete/src/main/java/com/kedia/swipetodelete/DragAndDrop.kt deleted file mode 100644 index 3e23133..0000000 --- a/SwipeToDelete/src/main/java/com/kedia/swipetodelete/DragAndDrop.kt +++ /dev/null @@ -1,114 +0,0 @@ -package com.kedia.swipetodelete - -import android.view.GestureDetector -import android.view.MotionEvent -import android.view.View -import androidx.recyclerview.widget.ItemTouchHelper -import androidx.recyclerview.widget.RecyclerView - -object DragAndDrop: View.OnTouchListener, GestureDetector.OnGestureListener { - - private lateinit var gestureDetector: GestureDetector - private lateinit var touchHelper: ItemTouchHelper - - private lateinit var holder: RecyclerView.ViewHolder - - fun RecyclerView.addDragToSwipe(): ItemTouchHelper { - - val itemTouchCallback = object : ItemTouchHelper.Callback() { - override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { - super.onSelectedChanged(viewHolder, actionState) - if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) { - viewHolder!!.itemView.alpha = 0.2f - } - } - - override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { - super.clearView(recyclerView, viewHolder) - viewHolder.itemView.alpha = 1f - } - - override fun isItemViewSwipeEnabled(): Boolean { - return false - } - - override fun isLongPressDragEnabled(): Boolean { - return false - } - - override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int { - holder = viewHolder - holder.itemView.setGestures() - val drags = - ItemTouchHelper.START or ItemTouchHelper.END or ItemTouchHelper.UP or ItemTouchHelper.DOWN - return makeMovementFlags( - drags, - 0 - ) - } - - override fun onMove( - recyclerView: RecyclerView, - viewHolder: RecyclerView.ViewHolder, - target: RecyclerView.ViewHolder - ): Boolean { - adapter?.notifyItemMoved(viewHolder.adapterPosition, target.adapterPosition) - return true - } - - override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {} - - } - - - val itemTouchHelper = ItemTouchHelper(itemTouchCallback) - itemTouchHelper.attachToRecyclerView(this) - touchHelper = itemTouchHelper - return itemTouchHelper - } - - - fun View.setGestures() { - gestureDetector = GestureDetector(this.context, this@DragAndDrop) - this.setOnTouchListener(this@DragAndDrop) - } - - override fun onTouch(v: View?, event: MotionEvent?): Boolean { - gestureDetector.onTouchEvent(event) - return true - } - - override fun onDown(e: MotionEvent?): Boolean { - return false - } - - override fun onShowPress(e: MotionEvent?) {} - - override fun onSingleTapUp(e: MotionEvent?): Boolean { - - return false - } - - override fun onScroll( - e1: MotionEvent?, - e2: MotionEvent?, - distanceX: Float, - distanceY: Float - ): Boolean { - return false - } - - override fun onLongPress(e: MotionEvent?) { - touchHelper.startDrag(holder) - } - - override fun onFling( - e1: MotionEvent?, - e2: MotionEvent?, - velocityX: Float, - velocityY: Float - ): Boolean { - return false - } - -} \ No newline at end of file diff --git a/SwipeToDelete/src/main/java/com/kedia/swipetodelete/SwipeToDelete.kt b/SwipeToDelete/src/main/java/com/kedia/swipetodelete/SwipeToDelete.kt index 626bbaf..d45aa2f 100644 --- a/SwipeToDelete/src/main/java/com/kedia/swipetodelete/SwipeToDelete.kt +++ b/SwipeToDelete/src/main/java/com/kedia/swipetodelete/SwipeToDelete.kt @@ -1,14 +1,21 @@ 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 = emptyList(), - listener: OnSwiped? = null + listener: OnSwiped? = null, + @ColorInt colorOneInt: Int? = null, + @ColorInt colorTwoInt: Int? = null ) { var swipeDirs = RIGHT @@ -28,14 +35,52 @@ object SwipeToDelete { } override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { - this@addSwipeToDelete.adapter?.notifyItemRemoved(viewHolder.adapterPosition) 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()) + + 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) } diff --git a/SwipeToDelete/src/main/res/drawable/ic_background.xml b/SwipeToDelete/src/main/res/drawable/ic_background.xml new file mode 100644 index 0000000..f4622a1 --- /dev/null +++ b/SwipeToDelete/src/main/res/drawable/ic_background.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/kedia/customswipelibrary/Adapter.kt b/app/src/main/java/com/kedia/customswipelibrary/Adapter.kt index f753b85..61e6360 100644 --- a/app/src/main/java/com/kedia/customswipelibrary/Adapter.kt +++ b/app/src/main/java/com/kedia/customswipelibrary/Adapter.kt @@ -9,7 +9,7 @@ import androidx.recyclerview.widget.RecyclerView class Adapter( private val context: Context, - private val list: List + private val list: MutableList ): RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder { @@ -24,6 +24,10 @@ class Adapter( holder.bind(list[position]) } + fun removeItem(adapterPosition: Int) { + list.removeAt(adapterPosition) + } + inner class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { private var textView = itemView.findViewById(R.id.textView) diff --git a/app/src/main/java/com/kedia/customswipelibrary/MainActivity.kt b/app/src/main/java/com/kedia/customswipelibrary/MainActivity.kt index e134c23..a807125 100644 --- a/app/src/main/java/com/kedia/customswipelibrary/MainActivity.kt +++ b/app/src/main/java/com/kedia/customswipelibrary/MainActivity.kt @@ -1,9 +1,11 @@ package com.kedia.customswipelibrary import android.os.Bundle +import android.util.Log import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import androidx.core.content.res.ResourcesCompat import androidx.recyclerview.widget.LinearLayoutManager -import com.kedia.swipetodelete.DragAndDrop.addDragToSwipe import com.kedia.swipetodelete.SwipeToDelete import com.kedia.swipetodelete.SwipeToDelete.addSwipeToDelete import kotlinx.android.synthetic.main.activity_main.* @@ -26,14 +28,12 @@ class MainActivity : AppCompatActivity(), SwipeToDelete.OnSwiped { adapter = this@MainActivity.adapter } - recycler.addDragToSwipe() - val list = listOf(SwipeToDelete.DIRECTION.LEFT, SwipeToDelete.DIRECTION.RIGHT) recycler.addSwipeToDelete(list, this) } override fun swipeToDelete(adapterPosition: Int) { - + adapter.removeItem(adapterPosition) } } \ No newline at end of file