5

I would like to create smth like that with using ItemTouchHelper: enter image description here

This picture was taken from my current project but it was created with SwipeRevealLayout and it is too difficult for me to customise it.Some days ago, I saw this question and understood that I can use ItemTouchHelper for solveing my problem. I found this code and tried to use it inside my project:

 val simpleCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
            override fun onMove(p0: RecyclerView, p1: RecyclerView.ViewHolder, p2: RecyclerView.ViewHolder): Boolean {
                return false
            }

            override fun getSwipeDirs(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
                val position = viewHolder.adapterPosition
                if (position != editPosition) {
                    if (editPosition != -1 && flagSwiped) {
                        adapter.notifyItemChanged(position)
                        flagSwiped = false
                        editPosition = position
                    }
                }
                return super.getSwipeDirs(recyclerView, viewHolder)
            }

            override fun onSwiped(holder: RecyclerView.ViewHolder, direction: Int) {

            }

            override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
                val icon: Drawable

                if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
                    val itemView = viewHolder.itemView;
                    val height = itemView.bottom - itemView.top;
                    val width = itemView.right - itemView.left;
                    val iconH = resources.displayMetrics.density * 28;
                    val iconW = resources.displayMetrics.density * 28;
                    if (dX > 0) {


                        val background = RectF(itemView.left.toFloat(), itemView.top.toFloat(), dX, itemView.bottom.toFloat())
                        p.color = Color.parseColor("#FF9800")
                        c.drawRect(background, p)

                        icon = ContextCompat.getDrawable(context!!, android.R.drawable.ic_input_add)!!
                        icon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)
                        // you can also use icon size
                        // int iconWidth = icon.getIntrinsicWidth();
                        // int iconHeight = icon.getIntrinsicHeight();

                        val rate = abs(dX) / width

                        val iconLeft = (itemView.left - iconW + width / 3 * rate).toInt()
                        val iconTop = (itemView.top + height / 2 - iconH / 2).toInt()
                        val iconRight = (itemView.left + width / 3 * rate).toInt()
                        val iconBottom = (itemView.bottom - height / 2 + iconH / 2).toInt()
                        icon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
                        icon.draw(c)

                    } else if (dX < 0) {

                        val background = RectF(itemView.right.toFloat() + dX, itemView.top.toFloat(), itemView.right.toFloat(), itemView.bottom.toFloat())
                        p.color = Color.parseColor("#E91E63")
                        c.drawRect(background, p)

                        icon = ContextCompat.getDrawable(context!!, android.R.drawable.ic_menu_delete)!!
                        icon.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP)

                        val rate = abs(dX) / width

                        val iconLeft = (itemView.right - width / 3 * rate).toInt()
                        val iconTop = (itemView.top + height / 2 - iconH / 2).toInt()
                        val iconRight = (itemView.right + iconW - width / 3 * rate).toInt()
                        val iconBottom = (itemView.bottom - height / 2 + iconH / 2).toInt()
                        icon.setBounds(iconLeft, iconTop, iconRight, iconBottom)
                        icon.draw(c)
                    }
                }

                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
            }
        }


        val itemTouchHelper = ItemTouchHelper(simpleCallback)
        itemTouchHelper.attachToRecyclerView(settings)

and here it the result of my work: enter image description here

as I can see I can swipe recyclerview items but I can't understand two things:

  1. How to add some more buttons like on the first screenshot?
  2. All items can be swiped once and I can't return to the normal state of item. I can only swipe and it is all.
Andrew
  • 1,416
  • 10
  • 35
  • with swipe reveal layout also you can have 3 buttons. You will have swipereveal layout as your root layout and you can have main layout in the front and then secondary layout in the back. Secondary layout can have 3 buttons on the side right? – Raghunandan Jul 18 '19 at 06:32
  • @Raghunandan, I understand it and swipereveallayout it the best solution, but I see that I can't close already swiped RV item and as the result when I scroll my list I see that some items become swiped but I don't swipe them – Andrew Jul 18 '19 at 06:38
  • @Raghunandan, I have implemented this type of layout but I have some problems with closing opened items from RV adapter – Andrew Jul 18 '19 at 06:38
  • No need to reinvent the wheel. Try [this](https://github.com/daimajia/AndroidSwipeLayout) – MidasLefko Jul 18 '19 at 06:39
  • @MidasLefko, I used this https://github.com/chthai64/SwipeRevealLayout/blob/master/swipe-reveal-layout/src/main/java/com/chauthai/swipereveallayout/SwipeRevealLayout.java layout, but I have a problem about which you can read at my previous comments :) – Andrew Jul 18 '19 at 06:41
  • @AndrewGoroshko check this https://www.androidhive.info/2017/09/android-recyclerview-swipe-delete-undo-using-itemtouchhelper/ if it helps. Note : I have not tried it at all. – Raghunandan Jul 18 '19 at 06:46
  • Just found this https://stackoverflow.com/questions/31787272/android-recyclerview-itemtouchhelper-revert-swipe-and-restore-view-holder. – Raghunandan Jul 18 '19 at 06:51

0 Answers0