Last active
November 15, 2020 14:38
-
-
Save vshkl/a9b5608e08b062b9f6f26cdd88a62b55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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