Skip to content

Commit

Permalink
Merge pull request #12 from Rere-kt/feature/item-touch-helper
Browse files Browse the repository at this point in the history
Added item touch helper
  • Loading branch information
dionep authored Jan 26, 2021
2 parents 4ed2112 + 83b619e commit 7b6574e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewDSL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ fun RecyclerView.configure(
layoutManager = it.layoutManager
this.adapter = adapter
it.itemDecorations.forEach { addItemDecoration(it) }
it.itemTouchHelper?.attachToRecyclerView(this)
}
}

Expand Down Expand Up @@ -46,6 +47,8 @@ class RecyclerViewConfig(
internal var layoutManager: RecyclerView.LayoutManager =
LinearLayoutManager(context)

internal var itemTouchHelper: ItemTouchHelper? = null

fun linearLayout(
block: LinearLayoutManager.() -> Unit = {}
) { layoutManager = LinearLayoutManager(context).apply(block) }
Expand All @@ -64,6 +67,24 @@ class RecyclerViewConfig(
)
}

fun itemTouchHelper(
dragFlags: Int = ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.RIGHT or ItemTouchHelper.LEFT,
swipeFlags: Int = 0,
onSwiped: (RecyclerView.ViewHolder, direction: Int) -> Unit = { _, _ -> },
onMove: (RecyclerView.ViewHolder, RecyclerView.ViewHolder) -> Boolean = { _, _ -> true }
) {
itemTouchHelper = ItemTouchHelper(
object : ItemTouchHelper.Callback() {
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int = makeMovementFlags(dragFlags, swipeFlags)
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean =
onMove(viewHolder, target)
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
onSwiped.invoke(viewHolder, direction)
}
}
)
}

fun dividerItemDecoration(
size: Int,
orientation: Int = DividerItemDecoration.VERTICAL
Expand Down

0 comments on commit 7b6574e

Please sign in to comment.