客服:“xxx,退货的按钮在哪啊,我怎么找不到啊?”我:“像Android 手机QQ一样侧滑就出来了。。。”。类似这样的对话重复上演了N次
没错,今天我要介绍的是和QQ一样的侧滑删除类似的操作,SwipeMenuLayout登场~~
最早收到的要实现Item侧滑删除的需求是在一个餐饮项目,当时刚入坑没多久,还以为SDK就有现成的API,毕竟像QQ这种超级App都用的这么骚了。于是一顿搜索之后,失望了,这尼玛是IOS上的效果啊。Android上实现了这种效果的App都是自定义View的
当时小白的自己心血来潮想着自己写个试试,一阵捣鼓下来结果弄了个四不像。最后还是尴尬的打开了GayHub,找了很多都是基于ListView或RecyclerView自定义的,这种感觉入侵性太强,所以都放弃了。最后找到了今天的主角SwipeMenuLayout,先看效果图

用法非常简单,当成普通的ViewGroup使用即可,首先我们先引到自己的项目,可以在app module的gralde引入
dependencies {
implementation 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'
}
也可以直接下载整个lib,这样的好处的是方便自己修改。其实只有一个类,还有就是style了
开始写布局,item很简单,一个content和两个按钮
<?xml version="1.0" encoding="utf-8"?>
<com.one.hope.widget.SwipeMenuLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true">
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical"
android:paddingStart="10dp"
android:paddingEnd="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="27dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
style="@style/add_text"
tools:text="姓名" />
<TextView
android:id="@+id/tv_phone"
style="@style/add_text"
android:layout_weight="2"
tools:text="联系方式" />
</LinearLayout>
<TextView
android:id="@+id/tv_address"
android:layout_width="match_parent"
android:layout_height="27dp"
android:gravity="center_vertical"
tools:text="地址" />
</LinearLayout>
<Button
android:id="@+id/btnUpdate"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:text="修改"
android:textColor="@android:color/white" />
<Button
android:id="@+id/btnDelete"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#ff4a57"
android:text="删除"
android:textColor="@android:color/white" />
</com.one.hope.widget.SwipeMenuLayout>
然后编写Activity,列表我选了RecyclerView,当然也可以用ListView(这就是SwipeMenuLayout的强大之处,0耦合)。Adapter使用的是BaseRecyclerViewAdapterHelper
此篇章旨在介绍有这个么强大又好用的东西,具体使用介绍和实现原理可以看作者的分析《侧滑删除菜单 SwipeMenuLayout》
欢迎关注我的个人微信公众号,【优了个秀】和你每天进步一点点