Переведите кнопки AndroidAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Переведите кнопки Android

Сообщение Anonymous »

Я хочу сделать удары, чтобы раскрыть с липкой и удалить логику без использования библиотек, но это не работает, как я понимаю, потому что DX ItemTouchHelper движется по сравнению с положением контента. Я поймаю ошибку в положении DeletethReshold из -за проблем с DX, которая становится большим. означает, что когда порог достигнут, элемент переработки должен быть заполнен основной кнопкой (первая кнопка в строке), а затем удален.

Код: Выделить всё






























< /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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»