Код: Выделить всё
< /code>
Вот swiperevealhelper: < /p>
class SwipeRevealHelper(
private val cardContent: (View) -> View,
private val cardBtns: (View) -> View,
private val mainBtn: (View) -> ImageButton,
private val otherBtns: (View) -> List,
private val percToSticky: Float,
private val percToDelete: Float
) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
private var baseBtnContainerWidth = 0f
private var baseBtnWidth = 0f
private var baseContentWidth = 0f
private var stickyThreshold = 0f
private var deleteThreshold = 0f
private var activePosition = RecyclerView.NO_POSITION
private var isSwipeDelete = false
private var isSwipeSticky = false
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
) = false
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
//TODO
}
override fun onChildDraw(
c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
val itemView = viewHolder.itemView
val cardContent = cardContent(itemView)
val cardBtns = cardBtns(itemView)
val mainBtn = mainBtn(itemView)
val otherBtns = otherBtns(itemView)
if (baseBtnContainerWidth == 0f) {
baseBtnWidth = mainBtn.width.toFloat()
baseBtnContainerWidth = cardBtns.width.toFloat()
baseContentWidth = cardContent.width.toFloat()
stickyThreshold = baseBtnContainerWidth * percToSticky
deleteThreshold = baseContentWidth * percToDelete
}
val swipeDistance = (-dX).coerceIn(0f, baseContentWidth)
if (isCurrentlyActive) {
cardContent.translationX = -swipeDistance
if (swipeDistance > baseBtnContainerWidth) {
val extraWidth = swipeDistance - baseBtnContainerWidth
mainBtn.layoutParams.width = (baseBtnWidth + extraWidth).toInt()
mainBtn.requestLayout()
} else {
mainBtn.layoutParams.width = baseBtnWidth.toInt()
mainBtn.requestLayout()
}
if (swipeDistance >= deleteThreshold) {
isSwipeDelete = true
isSwipeSticky = false
activePosition = viewHolder.bindingAdapterPosition
otherBtns.forEach { it.visibility = View.GONE }
mainBtn.layoutParams.width = cardContent.width
mainBtn.requestLayout()
cardContent.visibility = View.INVISIBLE
} else {
if (swipeDistance >= stickyThreshold) {
isSwipeDelete = false
isSwipeSticky = true
activePosition = viewHolder.bindingAdapterPosition
} else {
isSwipeDelete = false
isSwipeSticky = false
activePosition = RecyclerView.NO_POSITION
}
cardContent.visibility = View.VISIBLE
otherBtns.forEach { it.visibility = View.VISIBLE }
}
} else {
if (viewHolder.bindingAdapterPosition == activePosition) {
if (isSwipeDelete) {
animateTo(cardContent, 0f) {
isSwipeDelete = false
activePosition = RecyclerView.NO_POSITION
cardContent.visibility = View.VISIBLE
otherBtns.forEach { it.visibility = View.VISIBLE }
mainBtn.layoutParams.width = baseBtnWidth.toInt()
}
} else if (isSwipeSticky) {
animateTo(cardContent, -baseBtnContainerWidth) {
mainBtn.layoutParams.width = baseBtnWidth.toInt()
mainBtn.requestLayout()
}
}
} else {
animateTo(cardContent, 0f) {
activePosition = RecyclerView.NO_POSITION
isSwipeDelete = false
isSwipeSticky = false
}
}
}
super.onChildDraw(c, recyclerView, viewHolder, 0f, dY, actionState, isCurrentlyActive)
}
private fun animateTo(view: View, targetX: Float, endAction: (() -> Unit)? = null) {
val animator = view.animate()
.translationX(targetX)
.setDuration(150)
if (endAction != null) {
animator.withEndAction(endAction)
}
animator.start()
}
}
< /code>
Здесь вызов Swiperevealhelper из деятельности, где переработка: < /p>
val swipeHelper = SwipeRevealHelper(
cardContent = { it.findViewById(R.id.cardContent) },
cardBtns = { it.findViewById(R.id.itemBtns) },
mainBtn = { it.findViewById(R.id.btnDelete) },
otherBtns = { listOf(
it.findViewById(R.id.btnOptions),
it.findViewById(R.id.btnArchive)
) },
percToSticky = 0.5f,
percToDelete = 0.8f
)
ItemTouchHelper(swipeHelper).attachToRecyclerView(binding.projectList)
Подробнее здесь: https://stackoverflow.com/questions/797 ... ns-android