效果如图:
学习别人的程序源代码,发现这款开源框架,特此记录一下使用方法。
不知道博主是谁,所以找另外一个和此很相似的一个博客来学习:【Android】毫无耦合性,一个Item根布局搞定 item侧滑删除菜单,像IOS那样简单的使用侧滑删除。 - zxt0601的博客 - CSDN博客 http://blog.csdn.net/zxt0601/article/details/52303781
使用具体步骤:
1.在app的gradle中添加依赖:
在android中添加:
repositories {
maven {
url "https://jitpack.io"
}
}
在dependencies中添加
compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'
2.布局xml的使用:
在为列表项写布局文件时,直接在最外层使用SwipeMenuLayout ,内部第一个控件为正常显示的内容,从第二个控件开始就全部是侧滑显示的内容。
<?xml version="1.0" encoding="utf-8"?>
<com.mcxtzhang.swipemenulib.SwipeMenuLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/item_list_view"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
app:ios="false">
<!-- 在本控件内依次排列ContentItem、菜单即可-->
<!-- ContentItem为 TextView -->
<TextView
android:id="@+id/tv_sample_name"
android:singleLine="true"
android:ellipsize="end"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/app_sample_name"
android:textSize="@dimen/large_size"/>
<!-- 以下都是侧滑菜单的内容依序排列 -->
<Button
android:id="@+id/btn_edit_sample"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#d9dee4"
android:text="编辑"
android:textColor="@android:color/white"/>
<Button
android:id="@+id/btn_delete_sample"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@color/app_red"
android:text="删除"
android:textColor="@android:color/white"/>
</com.mcxtzhang.swipemenulib.SwipeMenuLayout>
holder.itemListView.smoothClose();//关闭侧滑效果,有反方向滑动动画效果
holder.itemListView.quickClose();//直接关闭侧滑,无动画效果
补充:
此框架可用于任何你想实现通过滑动操作后显示出相应控件的地方,举例如下:
效果图:
界面两个红色区域皆为textview;只有蓝色区域使用了框架。
布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="@color/colorAccent"/>
<com.mcxtzhang.swipemenulib.SwipeMenuLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/item_list_view"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
app:ios="false">
<!-- 在本控件内依次排列ContentItem、菜单即可-->
<!-- ContentItem为 TextView -->
<TextView
android:id="@+id/tv_sample_name"
android:singleLine="true"
android:ellipsize="end"
android:text="9999999"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- 以下都是侧滑菜单的内容依序排列 -->
<Button
android:id="@+id/btn_edit_sample"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#d9dee4"
android:text="编辑"
android:textColor="@android:color/white"/>
<Button
android:id="@+id/btn_delete_sample"
android:layout_width="60dp"
android:layout_height="match_parent"
android:text="删除"
android:textColor="@android:color/white"/>
</com.mcxtzhang.swipemenulib.SwipeMenuLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_marginTop="10dp"
android:background="@color/colorAccent"/>
</LinearLayout>