How to have swipe to delete and swipe to archive i

2019-08-13 19:47发布

问题:

Right now I have a funtional swipe left to delete in recyclerview with two layouts(foreground and background). I use itemtouchhelper in the code. However, I would like to have BOTH swipe left and swipe right showing different colors and icons, like in Google Inbox. How can I implement that?

What I want is: swipe rightswipe left

what I have is: only swipe right

the code is just the standard itemtouchhelper.simplecallback with 2 layouts in the xml. And I googled everywhere and only found single swipe option with single icon and single color

回答1:

Use ItemTouchHelper to Implement the Gmail Like Feature

call the following function after setting the recyclerView

private fun initSwipe() {
        val simpleItemTouchCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {

            override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition

                if (direction == ItemTouchHelper.LEFT) {

                   //Logic to do when swipe left

                } else {

                  //Logic to do when swipe right

                }
            }

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


                val icon: Bitmap
                if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {


       //Drawing for Swife Right    

                    val itemView = viewHolder.itemView
                    val height = itemView.bottom.toFloat() - itemView.top.toFloat()
                    val width = height / 3
                    if (dX > 0) {
                        p.color = Color.parseColor("#2F2FD3")
                        val background = RectF(itemView.left.toFloat(), itemView.top.toFloat(), dX, itemView.bottom.toFloat())
                        c.drawRect(background, p)
                        icon = BitmapFactory.decodeResource(resources, R.drawable.ic_archive)
                        val icon_dest = RectF(itemView.left.toFloat() + width, itemView.top.toFloat() + width, itemView.left.toFloat() + 2 * width, itemView.bottom.toFloat() - width)
                        c.drawBitmap(icon, null, icon_dest, p)
                    } else {

       //Drawing for Swife Left


                        p.color = Color.parseColor("#D32F2F")
                        val background = RectF(itemView.right.toFloat() + dX, itemView.top.toFloat(), itemView.right.toFloat(), itemView.bottom.toFloat())
                        c.drawRect(background, p)
                        icon = BitmapFactory.decodeResource(resources, R.drawable.ic_delete)
                        val icon_dest = RectF(itemView.right.toFloat() - 2 * width, itemView.top.toFloat() + width, itemView.right.toFloat() - width, itemView.bottom.toFloat() - width)
                        c.drawBitmap(icon, null, icon_dest, p)
                    }
                }
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
            }
        }
        val itemTouchHelper = ItemTouchHelper(simpleItemTouchCallback)
        itemTouchHelper.attachToRecyclerView(YOUR_RECYCLER_VIEW)
    }

Where Object p is an object of paint Paint p = new Paint() Hope this may help you.