SwipeDelMenuLayout

 

介绍:

一个Item根布局搞定 item侧滑删除菜单,像IOS那样简单的使用侧滑删除。

运行效果:

使用说明:

本控件不依赖任何父布局,不是针对 RecyclerView、ListView,而是任意的ViewGroup里的childView都可以使用侧滑(删除)菜单。

Step 1. 在项目根build.gradle文件中增加JitPack仓库依赖。

  1.     allprojects {
  2.         repositories {
  3.             ...
  4.             maven { url "https://jitpack.io" }
  5.         }
  6.     }

Step 2. Add the dependency

  1.     dependencies {
  2.             compile 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.2.2'
  3.     }

Step 3. 在需要侧滑删除的ContentItem外面套上本控件,在本控件内依次排列ContentItem、菜单即可:

至此 您就可以使用高仿IOS、QQ 侧滑删除菜单功能了 (侧滑菜单的点击事件等是通过设置的id取到,与其他控件一致,不再赘述)

Demo里,我的ContentItem是一个TextView,那么我就在其外嵌套本控件,并且以侧滑菜单出现的顺序,依次排列菜单控件即可。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <com.mcxtzhang.swipemenulib.SwipeMenuLayout
  3.     xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="100dp"
  6.     android:clickable="true"
  7.     android:paddingBottom="1dp">
  8.  
  9.     <TextView
  10.         android:id="@+id/content"
  11.         android:layout_width="match_parent"
  12.         android:layout_height="match_parent"
  13.         android:background="?android:attr/selectableItemBackground"
  14.         android:gravity="center"
  15.         android:text="项目中我是任意复杂的原ContentItem布局"/>
  16.  
  17.     <!-- 以下都是侧滑菜单的内容依序排列 -->
  18.     <Button
  19.         android:id="@+id/btnTop"
  20.         android:layout_width="60dp"
  21.         android:layout_height="match_parent"
  22.         android:background="#d9dee4"
  23.         android:text="置顶"
  24.         android:textColor="@android:color/white"/>
  25.  
  26.     <Button
  27.         android:id="@+id/btnUnRead"
  28.         android:layout_width="120dp"
  29.         android:layout_height="match_parent"
  30.         android:background="#ecd50a"
  31.         android:clickable="true"
  32.         android:text="标记未读"
  33.         android:textColor="@android:color/white"/>
  34.  
  35.     <Button
  36.         android:id="@+id/btnDelete"
  37.         android:layout_width="60dp"
  38.         android:layout_height="match_parent"
  39.         android:background="@color/red_ff4a57"
  40.         android:text="删除"
  41.         android:textColor="@android:color/white"/>
  42.  
  43. </com.mcxtzhang.swipemenulib.SwipeMenuLayout>

唯一注意事项: 若是在ListView、RecyclerView中使用,点击事件正确的设置应该是在 Adapter 里对 ContentItem 设置,不能使用listview.setOnItemClickListener。 因为此时 Item 是本控件了,不是里面的 ContentItem 那块区域了,且本控件区域有很多触摸的判断,内部包含 ContentItem 和侧滑菜单 Menu。

支持属性:

1 通过 isIos 变量控制是否是IOS阻塞式交互,默认是打开的。 2 通过 isSwipeEnable 变量控制是否开启右滑菜单,默认打开。(某些场景,复用item,没有编辑权限的用户不能右滑) 3 通过开关 isLeftSwipe支持左滑右滑

有两种方式设置: 一:xml:

  1. <com.mcxtzhang.swipemenulib.SwipeMenuLayout
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"
  3.     app:ios="false"
  4.     app:leftSwipe="true"
  5.     app:swipeEnable="true">

二: java代码:

//这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑  禁用掉侧滑菜单

  1. ((SwipeMenuLayout) holder.itemView).setIos(false).setLeftSwipe(position % 2 == 0 ? true : false).setSwipeEnable(false);

支持特性:

不会同时展开2+个侧滑菜单。(可见界面上最多只会出现一个侧滑菜单)。

侧滑过程中,禁止父控件上下滑动。

多指同时滑动,屏蔽后触摸的几根手指。

增加viewChache 的 get()方法,可以用在:当点击外部空白处时,关闭正在展开的侧滑菜单。

以第一个子Item(即ContentItem)的宽度为控件宽度

posted on 2018-03-29 15:41  TopCoder.NET  阅读(77)  评论(0)    收藏  举报