0

here is my problem that I have been trying to solve for a week now. When I perform a swipe to the left to reveal my buttons, when the view locks at maxleft, the click is only triggered after the third attempt. However, when I use "is currently active" and stop the swipe before maxleft, the click is triggered immediately. I have tried many different solutions, but none of them work. I also tried to implement a childraw over and clearview, but without any results. If you have any suggestions, I would be very grateful. Thank you.

I have tried to implement a clearview method with a condition if dx was less than maxswipe left and to bring back the carview and swippeButton to the position of -maxswipeleft, but it still requires 3 clicks. I also tried the notifydachanged in the on swipe; it works, but the swipe on the items is not performed in the correct position when I have more than 6 items. With the itemdatachanged, it takes two swipes to reveal the buttons.

I have simplified the layouts as much as possible, but I still have the same problem with three clicks. According to the logs, I think that when dx is less than maxleft, the swipe is considered finished, and I enter the onswipe function which requires 3 clicks to exit and update the position.

Any suggestions would be valuable. Thank you.

gidds
18.9k3 gold badges25 silver badges37 bronze badges
asked Apr 25, 2023 at 16:50
1
  • Please provide enough code so others can better understand or reproduce the problem. Commented Apr 25, 2023 at 17:04

1 Answer 1

0
 class ItemSwipeCallback(private val adapter: ListAdapter) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
 //var isSwiped = false
 override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
 if (viewHolder.itemViewType == R.layout.list_row) {
 return makeMovementFlags(0, 0)
 } else {
 val swipeFlags = ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
 return makeMovementFlags(0, swipeFlags)
 }
 }
 override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
 return true
 }
 override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float {
 return super.getSwipeThreshold(viewHolder)
 }
 override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
 //adapter.notifyDataSetChanged()
// val position = viewHolder.adapterPosition
// val item = adapter.itemList[position]
// adapter.notifyDataSetChanged()
 //adapter.notifyDataSetChanged()
// val position = viewHolder.adapterPosition
// adapter.notifyItemChanged(position)
 //isSwiped =true
// println("appel")
// return
 }
 override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
 if (viewHolder.itemViewType != TYPE_ITEM) {
 return
 }
// if (isSwiped == true){
// return
// }
 val cardView = viewHolder.itemView.findViewById<CardView>(R.id.cardView)
 val swipeButtons = viewHolder.itemView.findViewById<View>(R.id.swipe_buttons)
 if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
 val maxTranslationLeft = recyclerView.width * 0.66f
 var cardViewTranslationX = 0f
 if (dX < 0) {
 // Slide towards the left
 cardViewTranslationX = Math.max(dX, -maxTranslationLeft)
 cardView.translationX = cardViewTranslationX
 val swipeButtonsTranslationX = Math.min(0f, cardView.translationX + swipeButtons.width)
 swipeButtons.translationX = swipeButtonsTranslationX
 // Increase button width
 val swipeWidth = Math.abs(cardViewTranslationX)
 swipeButtons.layoutParams.width = swipeWidth.toInt()
 swipeButtons.requestLayout()
 println(swipeButtons.translationX)
 println("cardview ${cardView.translationX}")
 } else if (dX > 0) {
 // Swipe to right
 cardView.translationX = 0f
 }
 } else {
 super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
 }
 }
}
answered Apr 25, 2023 at 16:56
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.