美文网首页UI效果实用
android实现RecyclerView的Item侧滑菜单

android实现RecyclerView的Item侧滑菜单

作者: SeekLife0 | 来源:发表于2022-01-19 22:34 被阅读0次

    效果:

    效果

    实现步骤:

    1、引入组件

    implementation 'com.yanzhenjie.recyclerview:support:1.3.2'
    

    2、布局中添加组件
    使用该组件替换普通的RecyclerView即可

    <com.yanzhenjie.recyclerview.SwipeRecyclerView
                android:id="@+id/rv_message"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:overScrollMode="never"
                />
    

    3、activity中进行设置

            rvRecyclerView.adapter = null //先设置为null,否则会报错
            //------------------------------------------------------------------------------
            //创建菜单选项
            //注意:使用滑动菜单不能开启滑动删除,否则只有滑动删除没有滑动菜单
            var mSwipeMenuCreator = object : SwipeMenuCreator{
                //添加菜单自动添加至尾部
                override fun onCreateMenu(leftMenu: SwipeMenu?, rightMenu: SwipeMenu?, position: Int) {
                    var deleteItem = SwipeMenuItem(me)
                    deleteItem.setHeight(AutoSizeUtils.dp2px(me,55f)) //设置高度
                    deleteItem.setWidth(AutoSizeUtils.dp2px(me,70f)) //设置宽度
                    //通过shape来设置按钮的圆角
                    deleteItem.setBackground(R.drawable.shape_message_left) //设置圆角
                    //获取item的置顶标志,判断显示的字符
                    if(messageAdapter.getItem(position).isTop == 0){
                        deleteItem.setText("置顶")
                    }else{
                        deleteItem.setText("取消置顶")
                    }
                    deleteItem.setTextColor((Color.parseColor("#FFFFFFFF")))
                    //添加右侧菜单
                    rightMenu?.addMenuItem(deleteItem)
                    var addItem = SwipeMenuItem(me)
                    addItem.setHeight(AutoSizeUtils.dp2px(me,55f)) //设置高度
                    addItem.setWidth(AutoSizeUtils.dp2px(me,70f)) //设置宽度
                    addItem.setBackground(R.drawable.shape_message_right) //设置圆角
                    addItem.setImage(R.mipmap.message_delete)
                    //添加右侧菜单
                    rightMenu?.addMenuItem(addItem)
                    Log.e("侧滑菜单测试","侧滑菜单回调已经调用")
                }
            }
    
            //以下是设置菜单的点击事件,根据需要进行更改即可
            var mItemMenuClickListener = object : OnItemMenuClickListener {
                override fun onItemClick(menuBridge: SwipeMenuBridge?, adapterPosition : Int) {
                    // 任何操作必须先关闭菜单,否则可能出现Item菜单打开状态错乱。
                    menuBridge?.closeMenu()
                    // 左侧还是右侧菜单:
                    val direction = menuBridge!!.direction
                    // 菜单在Item中的Position:
                    val menuPosition = menuBridge.position
                    if (direction == SwipeRecyclerView.RIGHT_DIRECTION) {
                        when(menuPosition){
                            0 -> {
                                //置顶按钮,发起置顶请求
                                topRequest(messageAdapter.getItem(adapterPosition).messageId)
                            }
                            1 -> {
                                //删除按钮1、弹出删除二次检测对话框
                                //进行弹窗提示
                                CustomDialog.build().setMaskColor(Color.parseColor("#4D000000"))
                                    .setCustomView(object :
                                        OnBindView<CustomDialog>(R.layout.dialog_address_delete) {
                                        override fun onBind(dialog: CustomDialog?, v: View?) {
                                            val dialogText = v?.findViewById<TextView>(R.id.tv_content)
                                            val stvDelete = v?.findViewById<ShapeTextView>(R.id.stv_delete)
                                            val stvCancel = v?.findViewById<ShapeTextView>(R.id.stv_cancel)
                                            stvDelete?.text = "删除"
                                            stvDelete?.setTextColor(Color.parseColor("#FF3030"))
                                            dialogText?.text = "是否删除当前消息"
                                            stvCancel?.setOnClickListener {
                                                dialog?.dismiss()
                                                it.isClickable = false
                                                it.isEnabled = false
                                                Log.e("对话框取消按钮调试","-> 取消按钮已被按下")
                                            }
                                            stvDelete?.setOnClickListener {
                                                dialog?.dismiss()
    //                                            Log.e("对话框删除按钮调试","-> 删除按钮已被按下 -> 删除id${messageAdapter.getItem(adapterPosition).messageId}")
                                                deleteRequest(messageAdapter.getItem(adapterPosition).messageId)
                                                it.isClickable = false
                                                it.isEnabled = false
                                                //发出删除请求
                                            }
                                        }
                                    }).setDialogLifecycleCallback(object : DialogLifecycleCallback<CustomDialog>(){
                                        override fun onDismiss(dialog: CustomDialog?) {
                                            Log.e("对话框调试","对话框已关闭")
                                            super.onDismiss(dialog)
                                        }
                                    }).setAlign(CustomDialog.ALIGN.CENTER).show()
                            }
                        }
                    }
                }
            }
            rvRecyclerView.setSwipeMenuCreator(mSwipeMenuCreator)
            rvRecyclerView.setOnItemMenuClickListener(mItemMenuClickListener)
    

    相关文章

      网友评论

        本文标题:android实现RecyclerView的Item侧滑菜单

        本文链接:https://www.haomeiwen.com/subject/nvuzcrtx.html