Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swipe to delete #2

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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)

114 changes: 0 additions & 114 deletions SwipeToDelete/src/main/java/com/kedia/swipetodelete/DragAndDrop.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<DIRECTION> = emptyList(),
listener: OnSwiped? = null
listener: OnSwiped? = null,
@ColorInt colorOneInt: Int? = null,
@ColorInt colorTwoInt: Int? = null
) {

var swipeDirs = RIGHT
Expand All @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions SwipeToDelete/src/main/res/drawable/ic_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">

<stroke android:width="2dp"
android:color="@color/colorPrimaryDark" />

</shape>
6 changes: 5 additions & 1 deletion app/src/main/java/com/kedia/customswipelibrary/Adapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.recyclerview.widget.RecyclerView

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 @@ -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<TextView>(R.id.textView)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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)
}
}