5

我想使用 ItemTouchHelper 创建类似的东西: 在此处输入图像描述

这张照片是从我当前的项目中拍摄的,但它是用它创建的SwipeRevealLayout,我很难定制它。几天前,我看到了这个问题,明白我可以用它ItemTouchHelper来解决我的问题。我找到了这段代码并尝试在我的项目中使用它:

 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)

这是我工作的结果: 在此处输入图像描述

如我所见,我可以刷 recyclerview 项目,但我无法理解两件事:

  1. 如何在第一个屏幕截图中添加更多按钮?
  2. 所有物品都可以刷一次,我无法恢复物品的正常状态。我只能滑动,这就是全部。
4

0 回答 0