使用itemtouchhelper在滑动删除之前确认对话框

时间:2018-05-02 14:37:54

标签: android itemtouchhelper

如何在使用ItemTouchHelper删除Recyclerview Item之前显示确认对话框。 我创建了RecyclerView并实现了基本的ItemTouchHelper.Callback类。但是,我不知道下一步该做什么。

我创建了下面的类并将其附加到Recyclerview。刷掉项目有效。但我想在项目移动之前显示确认对话框。

class SwipeController extends Callback {

    public SwipeController( ) {

    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(0, START);
    }

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

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

    }
}

4 个答案:

答案 0 :(得分:2)

onSwiped中,使用正面和负面按钮添加AlertDialog,并对每个操作执行必要的操作。示例代码如下:

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
      AlertDialog.Builder builder = new AlertDialog.Builder(context);
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int arg1) {
          // Your action
          dialog.cancel();
        };
      });
      builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int arg1) {
          dialog.cancel();
        };
      });
    }

答案 1 :(得分:2)

这是我实施所需行为的方式,假设如果按“是”,则该项目将被删除,否则不执行任何操作:

new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        // Called when a user swipes left or right on a ViewHolder
        @Override
        public void onSwiped(final RecyclerView.ViewHolder viewHolder, int swipeDir) {
            // Here is where you'll implement swipe to delete
            new AlertDialog.Builder(viewHolder.itemView.getContext())
                    .setMessage("Are you sure?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // Get the position of the item to be deleted
                            int position = viewHolder.getAdapterPosition();
                            // Then you can remove this item from the adapter 
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog, 
                            // so we will refresh the adapter to prevent hiding the item from UI
                            mAdapter.notifyItemChanged(viewHolder.getAdapterPosition());
                        }
                    })
                    .create()
                    .show();
                }
            }).attachToRecyclerView(mRecyclerView);

答案 2 :(得分:0)

 @Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction, int position) {

    Log.d(TAG, "onSwiped() position=" + position + " direction=" + direction);

    // get item details
    final String itemIndex = cartList.get(viewHolder.getAdapterPosition()).getIndex();
    String itemSubjekt = cartList.get(viewHolder.getAdapterPosition()).getSubjekt();
    String itemPrio = cartList.get(viewHolder.getAdapterPosition()).getPrio();



    // from right to left = delete line
    if ((viewHolder instanceof CartListAdapter.MyViewHolder) && (direction == 4)){

        Log.d(TAG, "onSwiped() delete");

        Context context = viewHolder.itemView.getContext();

        AlertDialog adDelete = new AlertDialog.Builder(context).create();
        adDelete.setTitle(context.getResources().getString(R.string.dialog_delete_nadpis));
        adDelete.setMessage(context.getResources().getString(R.string.dialog_delete_really));

        adDelete.setButton(DialogInterface.BUTTON_POSITIVE, "ANO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        Log.d(TAG, "Dialog delete - vybrano ANO");

                        final Item deletedItem = cartList.get(viewHolder.getAdapterPosition());
                        // final int deletedIndex = viewHolder.getAdapterPosition();

                        // code here to delete item in db



                }
        });
        adDelete.setButton(DialogInterface.BUTTON_NEGATIVE, "NE", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            Log.d(TAG, "Dialog delete - vybrano NE");

                            mAdapter.notifyItemChanged(viewHolder.getAdapterPosition());

                        }
        });
        adDelete.show();


    }


    // from left to right = Edit line
    if ((viewHolder instanceof CartListAdapter.MyViewHolder) && (direction == 8)){

        Log.d(TAG, "onSwiped() edit");



    }


}

答案 3 :(得分:0)

感谢@AhmedAbdelaal

kotlin版本

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, i: Int) {
        if (enableConfirmDialogBeforeSwiped) {
            val alertDialogBuilder = AlertDialogBuilder(viewHolder.itemView.context)
            alertDialogBuilder.title("提示")
            alertDialogBuilder.message("确定删除该数据吗?")
            alertDialogBuilder.positiveButton("确定") {
                // remove this item
                mAdapter.onItemDismiss(viewHolder.adapterPosition)
            }
            alertDialogBuilder.negativeButton("取消") {
                // User cancelled the dialog, so we will refresh the adapter to prevent hiding the item from UI
                mAdapter.notifyItemChanged(viewHolder.adapterPosition)
            }
            alertDialogBuilder.show()
        } else {
            // remove this item
            mAdapter.onItemDismiss(viewHolder.adapterPosition)
        }
    }