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
  • 16,558
  • 2
  • 19
  • 26

1 Answers1

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)

        }

    }


}
  • 1
    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](/help/how-to-answer). – Community Apr 28 '23 at 07:44