3

如何在使用 ItemTouchHelper 删除 Recyclerview 项目之前显示确认对话框。我创建了 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

4 回答 4

10

这是我实现所需行为的方式,假设如果按下“是”,项目将被删除,否则什么都不做:

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);
于 2018-10-04T15:35:57.997 回答
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();
        };
      });
    }
于 2018-05-02T16:37:56.337 回答
0

感谢@AhmedAbdelaal

科特林版本

    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)
        }
    }
于 2019-10-15T08:25:03.400 回答
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");



    }


}
于 2018-12-11T09:01:07.050 回答