当前位置: 首页 > 知识库问答 >
问题:

要删除的RecyclerView滑动仍显示未完成滑动的可拉伸

戚星腾
2023-03-14

我的回收器视图使用ItemTouchHelper启用向右滑动功能,如下所示

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(createHelperCallback());
itemTouchHelper.attachToRecyclerView(myRecyclerView);

private ItemTouchHelper.Callback createHelperCallback() {
    return new ItemDragSwipeCallback(this, R.color.swipe_delete, R.drawable.ic_delete_black_24dp,
            0, ItemTouchHelper.RIGHT, new ItemDragSwipeCallback.OnTouchListener() {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            // delete code
        }
    });
}

自定义ItemTouchHelper.SimpleCallback

public class ItemDragSwipeCallback extends ItemTouchHelper.SimpleCallback {


    private Drawable mIcon;
    private final ColorDrawable mBackground;

    public interface OnTouchListener {
        boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target);

        void onSwiped(RecyclerView.ViewHolder viewHolder, int direction);
    }

    private OnTouchListener mOnTouchListener;

    public ItemDragSwipeCallback(Context context, int backgroundColor, int drawable, int dragDirs, int swipeDirs, OnTouchListener onTouchListener) {
        super(dragDirs, swipeDirs);
        mOnTouchListener = onTouchListener;
        mIcon = ContextCompat.getDrawable(context, drawable);
        mBackground = new ColorDrawable(context.getResources().getColor(backgroundColor));
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return mOnTouchListener.onMove(recyclerView, viewHolder, target);
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        mOnTouchListener.onSwiped(viewHolder, direction);
    }

    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

        View itemView = viewHolder.itemView;
        int backgroundCornerOffset = 25; //so mBackground is behind the rounded corners of itemView

        int iconMargin = (itemView.getHeight() - mIcon.getIntrinsicHeight()) / 2;
        int iconTop = itemView.getTop() + (itemView.getHeight() - mIcon.getIntrinsicHeight()) / 2;
        int iconBottom = iconTop + mIcon.getIntrinsicHeight();

        if (dX > 0) { // Swiping to the right
            int iconLeft = itemView.getLeft() + iconMargin;
            int iconRight = iconLeft + mIcon.getIntrinsicWidth();
            mIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom);

            mBackground.setBounds(itemView.getLeft(), itemView.getTop(),
                    itemView.getLeft() + ((int) dX) + backgroundCornerOffset, itemView.getBottom());
        } else if (dX < 0) { // Swiping to the left
            int iconLeft = itemView.getRight() - iconMargin - mIcon.getIntrinsicWidth();
            int iconRight = itemView.getRight() - iconMargin;
            mIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom);

            mBackground.setBounds(itemView.getRight() + ((int) dX) - backgroundCornerOffset,
                    itemView.getTop(), itemView.getRight(), itemView.getBottom());
        } else { // view is unSwiped
            mBackground.setBounds(0, 0, 0, 0);
        }

        mBackground.draw(c);
        mIcon.draw(c);
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我的问题是,当我假装滑动删除项目并改变主意时,可绘制的删除图标不会隐藏,直到我触摸屏幕进行任何其他操作。

更新:我找到了一个解决方法,将list_item的背景颜色更改为某种颜色。但是如果我需要透明背景,这不是正确的解决方案。

共有1个答案

郏志诚
2023-03-14

修改了自定义ItemTouchHelper的onChildRaw()。SimpleCallback通过使用mIcon取消擦除时将图标的边界设置为0。后置边界(0,0,0,0)

public class ItemDragSwipeCallback extends ItemTouchHelper.SimpleCallback {


    private Drawable mIcon;
    private final ColorDrawable mBackground;

    public interface OnTouchListener {
        boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target);

        void onSwiped(RecyclerView.ViewHolder viewHolder, int direction);
    }

    private OnTouchListener mOnTouchListener;

    public ItemDragSwipeCallback(Context context, int backgroundColor, int drawable, int dragDirs, int swipeDirs, OnTouchListener onTouchListener) {
        super(dragDirs, swipeDirs);
        mOnTouchListener = onTouchListener;
        mIcon = ContextCompat.getDrawable(context, drawable);
        mBackground = new ColorDrawable(context.getResources().getColor(backgroundColor));
    }

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
        return mOnTouchListener.onMove(recyclerView, viewHolder, target);
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        mOnTouchListener.onSwiped(viewHolder, direction);
    }

    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

        View itemView = viewHolder.itemView;
        int backgroundCornerOffset = 25; //so mBackground is behind the rounded corners of itemView

        int iconMargin = (itemView.getHeight() - mIcon.getIntrinsicHeight()) / 2;
        int iconTop = itemView.getTop() + (itemView.getHeight() - mIcon.getIntrinsicHeight()) / 2;
        int iconBottom = iconTop + mIcon.getIntrinsicHeight();

        if (dX > 0) { // Swiping to the right
            int iconLeft = itemView.getLeft() + iconMargin;
            int iconRight = iconLeft + mIcon.getIntrinsicWidth();
            mIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom);

            mBackground.setBounds(itemView.getLeft(), itemView.getTop(),
                    itemView.getLeft() + ((int) dX) + backgroundCornerOffset, itemView.getBottom());
        } else if (dX < 0) { // Swiping to the left
            int iconLeft = itemView.getRight() - iconMargin - mIcon.getIntrinsicWidth();
            int iconRight = itemView.getRight() - iconMargin;
            mIcon.setBounds(iconLeft, iconTop, iconRight, iconBottom);

            mBackground.setBounds(itemView.getRight() + ((int) dX) - backgroundCornerOffset,
                    itemView.getTop(), itemView.getRight(), itemView.getBottom());
        } else { // view is unSwiped
            mIcon.setBounds(0, 0, 0, 0);
            mBackground.setBounds(0, 0, 0, 0);
        }

        mBackground.draw(c);
        mIcon.draw(c);
    }
}

 类似资料:
  • 定义 模拟短信、微信聊天列表滑动删除的组件。 图片展示 代码演示 import SwipeAction from 'pile/dist/components/swipeaction' const {SwipeAction, Layouts} = pile, { Layout, LayoutHd, LayoutHdTitle, LayoutHdAside, LayoutBd, LayoutFt

  • 描述 (Description) 要删除滑动操作按钮,请按照说明操作 - 您可以使用 接下来,您可以通过向删除链接添加data-confirm和dataconfirm-title属性来确认删除。 要取消确认模型,您需要使用data-close-on-cancel属性。 例子 (Example) 以下示例演示如何使用swipeout删除Framework7中的操作按钮 - <!DOCTYPE htm

  • 我已经在我的应用程序中使用了上述代码。编辑部分工作正常,但问题是,只有当我们在单元格上从左向右滑动时,删除按钮才会出现。现在我有了一个菜单,可以像Facebook一样从左向右滑动。那么,我如何确保当用户从右向左滑动时,删除按钮出现呢。

  • 首先,我看到了这个问题:在使用Android的RecyclerView时,在滑动行下添加带有文本/图标的彩色背景 然而,即使标题说明“With text/icon”,答案也仅涵盖使用对象绘制矩形。 现在我已经实现了绘制绿色矩形;然而,我想让它更明显的是什么将发生的添加“完成”按钮,就像在下面的屏幕截图。 首先,它只画了一个绿色的小方块来包装我的“完成”图标(在下面的屏幕截图中看到的视图的红色部分来

  • 我曾经使用SwipeToDismiss库,但现在我正在尝试迁移到RecyclerView,但事情并不是那么明显,您知道这个库的任何替换吗?有什么想法如何从零开始实施它吗?