android 如何在到达一定距离后停止在回收站视图中滑动项目

qncylg1j  于 2023-02-06  发布在  Android
关注(0)|答案(2)|浏览(185)

我在Android中有一个回收视图。我在上面附加了一个ItemTouchHelper。我只启用了从左到右的滑动。
因此,当我滑动Recycler视图中的任何项目时,该项目开始向右移动,并在左侧绘制文本。这很好。我的要求是,我希望允许用户仅滑动一段距离,当达到该距离且用户释放触摸时,正在滑动的项目应返回到其向左的位置。
问题是,当我从左向右滑动时,视图会完全滑出屏幕。我如何限制它只在一定距离内滑动?我该怎么做?
下面是我的item touch回调代码:

private void initSwipe(){
    ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                int position = viewHolder.getAdapterPosition();
            }

        @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
            ViewHolder viewHolder1 = (ViewHolder)viewHolder;
            int position = viewHolder1.getAdapterPosition();
            MyItem item = dataList.get(position);
            if (item==null )return;
            if(actionState == ItemTouchHelper.ACTION_STATE_SWIPE){

                View itemView = viewHolder.itemView;
                float height = (float) itemView.getBottom() - (float) itemView.getTop();
                float width = height / 3;
                Paint p = new Paint();
                Paint textPaint = new Paint();
                textPaint.setColor(Color.WHITE);
                textPaint.setTextSize(20);

                if(dX > 0) {
                    p.setColor(Color.parseColor("#000000"));
                    RectF background = new RectF((float) itemView.getLeft(), (float) itemView.getTop(), dX,(float) itemView.getBottom());
                    c.drawRect(background,p);
                    //RectF icon_dest = new RectF((float) itemView.getLeft() + width ,(float) itemView.getTop() + width,(float) itemView.getLeft()+ 2*width,(float)itemView.getBottom() - width);
                    //c.drawBitmap(icon,null,icon_dest,p);
                    c.drawText(item.getDate(),(float)(itemView.getLeft() + width),(float)(itemView.getBottom() - width), textPaint);
                }
            }
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
    };
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(myRecycler);
}`
6l7fqoea

6l7fqoea1#

替换onChildDraw中的此行:

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

使用以下命令,它将最大x位置设置为100,根据需要调整此值。

float newDx = dX;
if (newDx >= 100f) {
    newDx = 100f
}
super.onChildDraw(c, recyclerView, viewHolder, newDx, dY, actionState, isCurrentlyActive);

Then,调用RecyclerView适配器上的notifyItemChanged。

@Override
 public void onSwiped(RecyclerView.ViewHolder viewHolder,
     int direction) {
         if (direction == ItemTouchHelper.RIGHT) {
        adapter.notifyItemChanged(viewHolder.getAdapterPosition())
    }
 }

notifyItemChanged的一个小问题是,当视图恢复到其原始位置时,您可能会注意到快速 Flink 。

vohkndzv

vohkndzv2#

在super.onChildDraw中,您可以使用以下命令:dX/5

override fun onChildDraw(
            c: Canvas,
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            dX: Float,
            dY: Float,
            actionState: Int,
            isCurrentlyActive: Boolean
        ) {
            if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
                val itemView = viewHolder.itemView
                val height = itemView.bottom.toFloat() - itemView.top.toFloat()
                val width = height / 3
                val p = Paint()
                if (dX < 0) {
                    p.color = Color.RED
                    val background = RectF(
                        itemView.right.toFloat() + dX,
                        itemView.top.toFloat(),
                        itemView.right.toFloat(),
                        itemView.bottom.toFloat()
                    )
                    c.drawRect(background, p)
                    val icon = BitmapFactory.decodeResource(
                        this@TimeLineFragment.resources,
                        R.drawable.ic_photo_delete
                    )
                    val margin = (dX / 5 - width) / 2
                    val iconDest = RectF(
                        itemView.right.toFloat() + margin,
                        itemView.top.toFloat() + width,
                        itemView.right.toFloat() + (margin + width),
                        itemView.bottom.toFloat() - width
                    )
                    c.drawBitmap(icon, null, iconDest, p)
                }
            } else {
                c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
            }

            super.onChildDraw(
                c,
                recyclerView,
                viewHolder,
                dX / 5,
                dY,
                actionState,
                isCurrentlyActive
            )
        }
    }).attachToRecyclerView(binding.timeLineList)

相关问题