This article has been authorized by the public number Guolin_blog (Guo Lin) Exclusive release
Reprint please indicate the source:
http://blog.csdn.net/zxt0601/article/details/53157090
This article is from: "Zhang Xudong's Blog" (http://blog.csdn.net/zxt0601)
Code Portal: If you like, point to a star. Thanks a lot
Https://github.com/mcxtzhang/SwipeDelMenuLayout
Important words at the beginning say, not for the Recyclerview or ListView, for the any viewgroup.
This control does not depend on any parent layout, not for Recyclerview, ListView, but for Childview in any viewgroup can use the slide-by (delete) menu.
Simple, 0 coupling, supports any viewgroup.
Overview
This control from the roll out of the project has been used for the past 7 months, the distance for the first time it was push to GitHub, also has February +. (I have published an article before.) Portal: http://blog.csdn.net/zxt0601/article/details/52303781, which describes in detail how the V1.0 version of this control is implemented. )
During a lot of friends in the comments, issue put forward some suggestions for improvement , such as support to set the sliding direction (left and right), high imitation QQ interaction, support Gridlayoutmanager, and some bugs. have been all my real, fixed . It is more convenient to pack it into the jitpack. Compared with the first edition, there are quite a few changes. So it will be sorted out, a new version.
Then this article starts with how to use it, and then describes the features it contains, the attributes it supports. Finally, a few difficulties and conflict resolution to explain.
Code Portal: If you like, point to a star. Thanks a lot
Https://github.com/mcxtzhang/SwipeDelMenuLayout
A few gifs to give you crossing feel the latest version of the charm (the following versions are shown by the way the optional two-way sliding)
The maximum charm of this control is 0 coupling, so first fit my other library assembly effect (Itemdecorationindexbar + swipemenulayout):
(Itemdecorationindexbar:https://github.com/mcxtzhang/itemdecorationindexbar)
Android Special Version (non-blocking, side-by-side menu expansion, you can still expand the other side-by-side menu, and the previous menu will automatically close):
Gridlayoutmanager (and the code ratio, just modify the Recyclerview LayoutManager. ):
LinearLayout (without any modification, even linearlayout can easily implement the side-slip menu):
IOS interaction (blocking interaction, high imitation QQ, Slide-by-menu expansion, masking all other item operations):
Use:
Step 1. Add Jitpack Warehouse Dependencies in the project root Build.gradle file.
allprojects { repositories { ... "https://jitpack.io" } } }
Step 2. Add the dependency
{ compile ‘com.github.mcxtzhang:SwipeDelMenuLayout:V1.2.1‘ }
Step 3. Set the control outside the ContentItem that need to be removed from the slide, and arrange the ContentItem within the control, and then click the menu:
At this point you can use the high imitation iOS, QQ slide to remove the menu function
(The Click event of the side-by-side menu is obtained by setting the ID, consistent with other controls, no longer repeat)
In the demo, my ContentItem is a textview, so I'm going to nest the controls, and arrange the menu controls in the order they appear in the slide-by menu.
<?xml version= "1.0" encoding= "Utf-8"?><com.mcxtzhang.swipemenulib.SwipeMenuLayoutxmlns:android="http// Schemas.android.com/apk/res/android "android:layout_width=" Match_parent "android: Layout_height="100DP"android:clickable="true"Android:paddingbottom ="1DP"> <TextViewandroid:id= "@+id/content"android:layout_width="Match_ Parent "android:layout_height="match_parent "android:background="? Android: Attr/selectableitembackground "android:gravity=" center "android:text= "I am an arbitrarily complex original ContentItem layout in the project"/> <!--The following are the contents of the side-slip menu, sorted by <button android:id = "@+id/btntop" android:layout_width = "60DP" android:layout_height = "match_parent" android:background = "#d9dee4" android:text =" pinned " android: TextColor = "@android: Color/white" /> <button android:id = "@+id/btnunread" android:layout_width = "120DP" android:layout_height = "match_parent" android:background =" #ecd50a " android:clickable =" True " android:text =" Mark Unread " android:textcolor = "@android: Color/white" /> <buttonandroid:id="@+id/btndelete"android:layout_width="60DP" android:layout_height="Match_parent"android:background="@color/red_ff4a57" Android:text="Delete"android:textcolor="@android: Color/white"/ > </com.mcxtzhang.swipemenulib.SwipeMenuLayout>
Supported Properties:
1 Controls whether iOS is blocking interaction via the Isios variable, which is turned on by default.
2 Controls whether the right-hand menu is turned on by isswipeenable variable. (Some scenarios, reusing item, users without edit permissions cannot be right-sliding)
3 support left slide right slide through switch isleftswipe
There are two ways of setting:
One: xml:
<com.mcxtzhang.swipemenulib.SwipeMenuLayout xmlns:app="http://schemas.android.com/apk/res-auto" app:ios="false" app:leftSwipe="true" app:swipeEnable="true">
Two: Java code:
//这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑 禁用掉侧滑菜单((SwipeMenuLayout) holder.itemView).setIos(false20truefalse).setSwipeEnable(false);
Support Features:
- You will not expand the two-side slide menu at the same time. (Only one side-slip menu appears on the visible interface).
- Prevents the parent control from sliding up or down during a slide.
- Multi-finger swipe at the same time, shielding after touching a few fingers.
- Add Viewchache to the Get () method, which can be used when clicking on an external space to close the slide-out menu that is being expanded.
- The width of the first child item (that is, ContentItem) is the control width
Checklist for each update:
Due to the continuous iteration, a bug that occurs after completing a feature, fix, causes a new bug.
So, make a copy of the checklist for verification after each iteration, and then push it to the GitHub library.
| Project |
comments |
validation |
| isios |
Switch to iOS blocking interactive mode, Android features non-blocking interactive mode The following feature are working correctly |
|
| isswipeenable |
whether to turn off the slide-by feature |
|
| isleftswipe |
support bidirectional swipe |
|
| ContentItem Content You can click |
|
|
| ContentItem content can be long press |
|
|
| when a side-by-side menu is displayed, ContentItem is not clickable |
|
|
| When the slide menu is displayed, ContentItem not long press |
|
|
| side-by-side menu can be clicked |
|
|
| When the side-slip menu is displayed, click the ContentItem area to close the menu |
|
|
| Slide-In process, shield long press event |
|
| by swiping the menu off, ContentItem Click events should not be triggered |
|
|
Difficulties and conflict resolution:
1 ContentItem a conflict between a long press and the slide of this control.
At first, I was still the old idea, always by judging the position of the finger starting point of the coordinates, to determine where the finger is located, shielding some operations. When this control becomes more and more powerful, the calculations begin to be complex and error-prone. But also stumbled over, but just today, I think of another way of thinking: by disabling the Longclickable property of the sub-view to complete this function. So I refactor this part of the code, first using Git to see the previous commit, to get rid of that part of the code, and
In the slide smoothExpand() -off menu, expand, Close smoothClose() the function to add:
//2016 11 13 add 侧滑菜单展开,屏蔽content长按 if (null != mContentView) { mContentView.setLongClickable(true); }
//2016 11 13 add 侧滑菜单展开,屏蔽content长按 if (null != mContentView) { mContentView.setLongClickable(true); }
The code was so simple that I thought of the simple solution a few hours ago, which is one of the reasons that prompted me to go through the new article and record some of the changes in the control.
2 How to support any parent control
This is one of the coolest charms of this control, and in the previous article I described the implementation process in detail.
Summing up, I used a static variable to save the currently unfolding view,
In the case of various operations can be pre-determined, if it will cause conflict, such as the interface appears two side-by-side menu,
The previous menu is automatically closed.
The code is as follows:
//存储的是当前正在展开的View privatestatic SwipeMenuLayout mViewCache;
dispatchTouchEvent()In the Actiondown:
//如果down,view和cacheview不一样,则立马让它还原。且把它置为null ifnull) { ifthis) { mViewCache.smoothClose(); } //只要有一个侧滑菜单处于打开状态, 就不给外层布局上下滑动了 getParent().requestDisallowInterceptTouchEvent(true); }
Functions to expand and close in the slide smoothExpand() -off menu smoothClose() :
//展开就加入ViewCache: mViewCache = SwipeMenuLayout.this;
//关闭置为null null;
onDetachedFromWindow()In
//每次ViewDetach的时候,判断一下 ViewCache是不是自己,如果是自己,关闭侧滑菜单,且ViewCache设置为null, // 理由:1 防止内存泄漏(ViewCache是一个静态变量) // 2 侧滑删除后自己后,这个View被Recycler回收,复用,下一个进入屏幕的View的状态应该是普通状态,而不是展开状态。 @Override protectedvoidonDetachedFromWindow() { if (this == mViewCache) { mViewCache.smoothClose(); null; } super.onDetachedFromWindow(); }
3 solving multiple-finger sliding collisions:
Use a Boolean flag to determine whether to continue accepting touch events when Actiondown:
The code is as follows:
//防止多只手指一起滑我的flag 在每次down里判断, touch事件结束清空 privatestaticboolean isTouching;
dispatchTouchEvent()In the Actiondown:
if (isTouching) {//如果有别的指头摸过了,那么就returnfalse。这样后续的move..等事件也不会再来找这个View了。 returnfalse; else { true;//第一个摸的指头,赶紧改变标志,宣誓主权。 }
In Actionup:
false;//没有手指在摸我了
4 Support Gridlayoutmanager
After all, the use of a side-slip menu in a grid layout in a project is a minority, so at first I simplified the scene, and the width of the control was set to the width of the parent control-padding. Later, there are children's shoes to support grid layout, at first I took a detour, I also want to build a matchparent measurespec, and then passed to the parent control (GridView, Recyclerview) for measurement.
The correct idea is to take the first sub-view, that is, the width of the contentview as the width of the control, so that when the layout side-off menu, automatically layout the side-slip menu in the invisible area, only by sliding to display it.
The code has nothing to say:
In the onMeasure() :
@Override protected void onmeasure(intWidthmeasurespec,intHEIGHTMEASURESPEC) {Super. Onmeasure (Widthmeasurespec, Heightmeasurespec);//2016 Add, adapted to Gridlayoutmanager, will be the width of the first child item (that is, ContentItem) as the control width intContentwidth =0;intChildCount = Getchildcount (); for(inti =0; i < ChildCount; i++) {View Childview = Getchildat (i);if(Childview.getvisibility ()! = GONE) {Measurechildwithmargins (Childview, Widthmeasurespec,0, Heightmeasurespec,0);FinalMarginlayoutparams LP = (marginlayoutparams) childview.getlayoutparams (); Mheight = Math.max (Mheight, childview.getmeasuredheight () + Lp.topmargin + lp.bottommargin);if(I >0) {//The first layout is left item, which is Rightmenu from the second startMrightmenuwidths + = Childview.getmeasuredwidth (); }Else{contentwidth = Childview.getmeasuredwidth (); }}} setmeasureddimension (Contentwidth, mheight);//width take the width of the first item (Content)}
In onLayout() , the order Layoutchildview can.
Summarize
The above is the control of the recent or interested in some of the points of detail, more detailed explanation can go to the previous article to watch, or go to GitHub download source browsing.
Above address:
http://blog.csdn.net/zxt0601/article/details/52303781
Code Portal: If you like, point to a star. Thanks a lot
Https://github.com/mcxtzhang/SwipeDelMenuLayout
If you have any questions in use, please feedback.
"Android" History of the simplest, one-step integrated slide (delete) menu, high imitation qq, IOS