Android 仿QQ侧滑item点击删除按钮,删除数据

https://blog.csdn.net/yijiaodingqiankun/article/details/80445892
如果想出现两个按钮,用以下代码

    // 设置菜单监听器。
    SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() { // 创建菜单:
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width = getResources().getDimensionPixelSize(R.dimen.dp_76);
            int height = ViewGroup.LayoutParams.MATCH_PARENT;

            SwipeMenuItem stocksItem = new SwipeMenuItem(getActivity())
                    .setBackground(R.color.c_00FF00)
                    .setTextColor(Color.WHITE)
                    .setText("标记为已完成")
                    .setWidth(width)
                    .setHeight(height);
            swipeRightMenu.addMenuItem(stocksItem);

            @SuppressLint("ResourceAsColor") SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity())
//                    .setBackground(R.drawable.selector_red)
                    .setBackground(R.color.c_ff0000)
//                    .setImage(R.mipmap.ic_action_delete)
                    .setTextColor(Color.WHITE)
                    .setText(R.string.delete)
                    .setWidth(width)
                    .setHeight(height);
            swipeRightMenu.addMenuItem(deleteItem);

        }
    };
    // 菜单点击监听。
    SwipeMenuItemClickListener swipeMenuItemClickListener = new SwipeMenuItemClickListener() {
        @Override
        public void onItemClick(SwipeMenuBridge menuBridge) { // 任何操作必须先关闭菜单,否则可能出现Item菜单打开状态错乱。
            menuBridge.closeMenu();
            int direction = menuBridge.getDirection();//左边还是右边菜单
            final int adapterPosition = menuBridge.getAdapterPosition();//    recyclerView的Item的position。
            int position = menuBridge.getPosition();// 菜单在RecyclerView的Item中的Position。

            if (position == 0){
                showLoading();
                presenter.doneTodo(mRecyclerViewList.get(adapterPosition).getId(),1);
                mRecyclerViewList.remove(adapterPosition);//删除item
                mMyBacklogRvAdapter.notifyDataSetChanged();
            }else {
                new AlertDialog.Builder(getActivity())
                        .setTitle("确认删除?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                showLoading();
//                                Log.e(TAG, "删除数据的索引: " + adapterPosition);
                                presenter.deleteTodo(mRecyclerViewList.get(adapterPosition).getId());

                                mRecyclerViewList.remove(adapterPosition);//删除item
                                mMyBacklogRvAdapter.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("取消",null)
                        .show();
            }
        }
    };
仿QQ.png

你可能感兴趣的:(Android 仿QQ侧滑item点击删除按钮,删除数据)