Skip to content

Instantly share code, notes, and snippets.

@vshkl
Last active November 15, 2020 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vshkl/a9b5608e08b062b9f6f26cdd88a62b55 to your computer and use it in GitHub Desktop.
Save vshkl/a9b5608e08b062b9f6f26cdd88a62b55 to your computer and use it in GitHub Desktop.
class MainEpoxyTouchCallback(
controller: MainController,
private val listener: OnRowMoveListener
) : EpoxyModelTouchCallback<RowEpoxyModel>(controller, RowEpoxyModel::class.java) {
interface OnRowMoveListener {
fun onMoved(movingRowId: String, shiftingRowId: String)
}
override fun onMoved(
recyclerView: RecyclerView?,
viewHolder: EpoxyViewHolder?,
fromPos: Int,
target: EpoxyViewHolder?,
toPos: Int,
x: Int,
y: Int
) {
val movingRowId = viewHolder?.model
?.takeIf { it is RowEpoxyModel_ }
?.let { (it as RowEpoxyModel_).rowId }
val shiftingRowId = target?.model
?.takeIf { it is RowEpoxyModel_ }
?.let { (it as RowEpoxyModel_).rowId }
if (movingRowId != null && shiftingRowId != null) {
listener.onMoved(movingRowId, shiftingRowId)
}
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y)
}
override fun getMovementFlagsForModel(model: RowEpoxyModel?, adapterPosition: Int) =
makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0)
override fun isLongPressDragEnabled() = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment