立即滑动时滑动以删除回收者视图删除项目

时间:2015-09-01 04:37:32

标签: android android-recyclerview

我已使用新的ItemTouchHelper.SimpleCallback

编写了Swipe来删除回收站视图

我的问题是当我滑动项目时出现一个对话框,当我说是,然后只从回收者视图中删除视图。但是现在在对话框出现之前删除了视图。这里有什么我想念的。

    public class SwipeToDeleteRecyclerView extends RecyclerView {

    private static final String TAG = SwipeToDeleteRecyclerView.class.getSimpleName();


    private OnSwipeListener onSwipeListener;

    public SwipeToDeleteRecyclerView(Context context) {
        super(context);
        init();
    }

    public SwipeToDeleteRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SwipeToDeleteRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    private void init() {
        SwipeSimpleCallback swipeCallback = new SwipeSimpleCallback();
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(swipeCallback);
        itemTouchHelper.attachToRecyclerView(this);
    }

    public void setOnSwipeListener(OnSwipeListener onSwipeListener) {
        this.onSwipeListener = onSwipeListener;
    }

    public OnSwipeListener getSwipeListener() {
        return onSwipeListener;
    }

    private class SwipeSimpleCallback extends ItemTouchHelper.SimpleCallback {
        private final String TAG = SwipeSimpleCallback.class.getSimpleName();

        public SwipeSimpleCallback() {
            this(0, ItemTouchHelper.LEFT);
        }

        SwipeSimpleCallback(int dragDirs, int swipeDirs) {
            super(dragDirs, swipeDirs);
        }

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

        @Override
        public void onSwiped(ViewHolder viewHolder, int direction) {

            if (onSwipeListener != null) {
                onSwipeListener.onSwipe(viewHolder, direction);
            } else {
                LogUtil.warn(TAG, "No Swipe listener attached to " + SwipeToDeleteRecyclerView.class.getSimpleName());
            }
        }
    }
}

这就是我设置回调的方式

recyclerView.setOnSwipeListener(new OnSwipeListener() {
        @Override
        public void onSwipe(RecyclerView.ViewHolder viewHolder, int swipeDirection) {
            position = viewHolder.getAdapterPosition();
            showConfirmDialog();
        }
    });

1 个答案:

答案 0 :(得分:0)

尝试在onSwiped()上设置对话框;

例如:

public void onSwiped(final RecyclerView.ViewHolder viewHolder, int swipeDir) {
    final int position = viewHolder.getAdapterPosition();
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
    alertDialogBuilder.setMessage("Are you sure that you want to remove it?");
    alertDialogBuilder.setPositiveButton("remove", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            recyclerView.getAdapter().notifyItemRemoved(position);
        }
    });
    alertDialogBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            clearView(recyclerView, viewHolder);
        }
    });
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();

}