Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

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,7 +52,8 @@ object DragAndDrop: View.OnTouchListener, GestureDetector.OnGestureListener {
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
(adapter as ItemTouchHelperAdapter).onItemMove(viewHolder.adapterPosition, target.adapterPosition)
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)
}

}

This file was deleted.

This file was deleted.

62 changes: 9 additions & 53 deletions app/src/main/java/com/kedia/customswipelibrary/Adapter.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.kedia.customswipelibrary

import android.content.Context
import android.util.Log
import android.view.GestureDetector
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.kedia.swipetodelete.ItemTouchHelperAdapter

class Adapter(
private val context: Context,
private val list: List<String>
private val list: MutableList<String>
): RecyclerView.Adapter<Adapter.CustomViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
Expand All @@ -26,63 +26,19 @@ class Adapter(
holder.bind(list[position])
}

inner class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
// GestureDetector.OnGestureListener, View.OnTouchListener
{
fun moveItem(positionStart: Int, positionEnd: Int) {
val temp = list[positionEnd]
list[positionEnd] = list[positionStart]
list[positionStart] = temp
}

inner class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

private var textView = itemView.findViewById<TextView>(R.id.textView)
private lateinit var gestureDetector: GestureDetector

fun bind(string: String) {
textView.text = string
// textView.setGestures()
// gestureDetector = GestureDetector(itemView.context, this)
// itemView.setOnTouchListener(this)
}

// 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?) {
// Log.d("TAG!!!!","called long")
// touchHelper?.startDrag(this)
// }
//
// override fun onFling(
// e1: MotionEvent?,
// e2: MotionEvent?,
// velocityX: Float,
// velocityY: Float
// ): Boolean {
// return false
// }

// override fun onTouch(v: View?, event: MotionEvent?): Boolean {
// gestureDetector.onTouchEvent(event)
// return true
// }

}

// override fun onItemMove(fromPosition: Int, toPosition: Int) {
// notifyItemMoved(fromPosition, toPosition)
// }

}
18 changes: 7 additions & 11 deletions app/src/main/java/com/kedia/customswipelibrary/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.kedia.customswipelibrary

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.kedia.swipetodelete.SwipeToDelete
import com.kedia.swipetodelete.DragAndDrop
import com.kedia.swipetodelete.DragAndDrop.addDragToSwipe
import com.kedia.swipetodelete.SwipeToDelete.addSwipeToDelete
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), SwipeToDelete.OnSwiped {
class MainActivity : AppCompatActivity(), DragAndDrop.onDragged {

private val list = mutableListOf<String>()
private lateinit var adapter: Adapter
Expand All @@ -26,15 +25,12 @@ class MainActivity : AppCompatActivity(), SwipeToDelete.OnSwiped {
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = this@MainActivity.adapter
}
recycler.addDragToSwipe(this)

recycler.addDragToSwipe()

val list = listOf(SwipeToDelete.DIRECTION.LEFT,
SwipeToDelete.DIRECTION.RIGHT)
recycler.addSwipeToDelete(list, this)
}

override fun swipeToDelete(adapterPosition: Int) {
Log.d("TAG!!!!", adapter.itemCount.toString())
override fun onPositionDragged(positionStart: Int, positionEnd: Int) {
adapter.moveItem(positionStart, positionEnd)
}

}