欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义view系列之99.99%实现QQ侧滑删除效果实例代码详解

程序员文章站 2024-03-06 20:28:14
首先声明本文是基于github上"baoyongzhang"的swipemenulistview修改而来,该项目地址: https://github.com/baoyon...

首先声明本文是基于github上"baoyongzhang"的swipemenulistview修改而来,该项目地址:

https://github.com/baoyongzhang/swipemenulistview

可以说这个侧滑删除效果是我见过效果最好且比较灵活的项目,没有之一!!!

但是在使用它之前需要给大家提两点注意事项:

1,该项目支持gradle dependence,但是目前作者提供的依赖地址对应的项目不是最新的项目,依赖过后的代码与demo中使用的不一致,会提示没有baseswipelistadapter这个类,因为这个类是其他的开发者后来提交上去的,所以如果想使用最新的代码,目前还是得把代码下载下来,然后把library文件拷贝到自己项目中使用.

下图是目前作者提供的依赖地址,不是最新的,所以想用最新代码的朋友还是直接下载代码到本地吧.

Android自定义view系列之99.99%实现QQ侧滑删除效果实例代码详解

2,第二点注意事项应该算是一个bug吧,如果你测试过作者给的demo,你会发现如果某一项item已经被拉出来了,这个时候你再把listview向上或向下滑动,让这个被拉出来的item移出屏幕,然后再移回来,这个已经被拉出来的item会直接恢复到未拉出的状态.这会让用户感觉很困惑,我明明已经拉出了这个菜单,怎么又不见了,然后可能就会产生这个软件做的真垃圾的想法,进而可能把你的软件卸载掉.如下图:

Android自定义view系列之99.99%实现QQ侧滑删除效果实例代码详解

对于上面两个注意事项,第一个倒是没什么好说的,第二个问题怎么办呢?别急,这正是我们今天要说的内容.

首先我们可以先研究一下qq的侧滑删除效果,说到这你可以打开你的qq看看它的具体效果.

你会发现,如果一个item被拉出来了,当你的手指放到其他的item上时,它会直接先把被拉出的那个item关闭掉,并且当前动作的后续的事件也都不再响应,除非你再次把手指放到屏幕上,他才会响应相关事件,而如果你的手指放到当前被拉出的item上,他不会隐藏这个item,并且可以正常响应左右滑动事件.

ok,qq的效果我们分析完毕,我们探讨一下它的实现原理:

1,如果一个item已经被拉了出来,当你的手指放到其他的item上时,它会直接先把被拉出的那个item关闭掉, 怎么实现呢?

首先我们需要判断我们当前所按下的这个item是不是被拉出来的那个item,不是的话,我们才需要关闭,是的话,则不用管.代码如下:

if (view instanceof swipemenulayout) { 
swipemenulayout touchview = (swipemenulayout) view; 
if (!touchview.isopen()) { 
mtouchview.smoothclosemenu(); 
} 
}

2,并且当前动作的后续的事件也都不再响应, 怎么实现呢?

这就很简单了,根据view的事件分发原理,如果在某一个触摸事件中返回了false,那么该事件后续的事件都不会再交给他处理,也就是说,如果我们在action_down的时候返回了false,那么后续的action_move,action_up等事件都不会响应,所以要实现这个效果,我们只需要在关闭菜单的后面,返回false就行了,完整的代码如下:

/********新添加的内容,当按下的item不是当前已经打开的item,则关闭已经打开的item,并返回false.不再响应down以后的事件,仿qq效果********/ 
if (view instanceof swipemenulayout) { 
swipemenulayout touchview = (swipemenulayout) view; 
if (!touchview.isopen()) { 
mtouchview.smoothclosemenu(); 
return false; 
} 
}

这样的几行代码就实现了刚才分析的qq效果中的前半部分效果,即如果一个item被拉出来了,当你的手指放到其他的item上时,它会直接先把被拉出的那个item关闭掉,并且当前动作的后续的事件也都不再响应,除非你再次把手指放到屏幕上,他才会响应相关事件.

上面的那几行代码是基于swipemenulistview类修改而来,完整的修改之后的swipemenulistview代码如下所示:

package com.lanma.swipemenulistviewdemo.swipemenulistview; 
import android.content.context; 
import android.util.attributeset; 
import android.util.typedvalue; 
import android.view.motionevent; 
import android.view.view; 
import android.view.animation.interpolator; 
import android.widget.listadapter; 
import android.widget.listview; 
/** 
* @author baoyz 
* @date 2014-8-18 
* qiang_xi修改于2016-09-07(新增qq的效果) 
*/ 
public class swipemenulistview extends listview { 
private static final int touch_state_none = 0; 
private static final int touch_state_x = 1; 
private static final int touch_state_y = 2; 
public static final int direction_left = 1; 
public static final int direction_right = -1; 
private int mdirection = 1;//swipe from right to left by default 
private int max_y = 5; 
private int max_x = 3; 
private float mdownx; 
private float mdowny; 
private int mtouchstate; 
private int mtouchposition; 
private swipemenulayout mtouchview; 
private onswipelistener monswipelistener; 
private swipemenucreator mmenucreator; 
private onmenuitemclicklistener monmenuitemclicklistener; 
private onmenustatechangelistener monmenustatechangelistener; 
private interpolator mcloseinterpolator; 
private interpolator mopeninterpolator; 
public swipemenulistview(context context) { 
super(context); 
init(); 
} 
public swipemenulistview(context context, attributeset attrs, int defstyle) { 
super(context, attrs, defstyle); 
init(); 
} 
public swipemenulistview(context context, attributeset attrs) { 
super(context, attrs); 
init(); 
} 
private void init() { 
max_x = dp2px(max_x); 
max_y = dp2px(max_y); 
mtouchstate = touch_state_none; 
} 
@override 
public void setadapter(listadapter adapter) { 
super.setadapter(new swipemenuadapter(getcontext(), adapter) { 
@override 
public void createmenu(swipemenu menu) { 
if (mmenucreator != null) { 
mmenucreator.create(menu); 
} 
} 
@override 
public void onitemclick(swipemenuview view, swipemenu menu, 
int index) { 
boolean flag = false; 
if (monmenuitemclicklistener != null) { 
flag = monmenuitemclicklistener.onmenuitemclick( 
view.getposition(), menu, index); 
} 
if (mtouchview != null && !flag) { 
mtouchview.smoothclosemenu(); 
} 
} 
}); 
} 
public void setcloseinterpolator(interpolator interpolator) { 
mcloseinterpolator = interpolator; 
} 
public void setopeninterpolator(interpolator interpolator) { 
mopeninterpolator = interpolator; 
} 
public interpolator getopeninterpolator() { 
return mopeninterpolator; 
} 
public interpolator getcloseinterpolator() { 
return mcloseinterpolator; 
} 
@override 
public boolean onintercepttouchevent(motionevent ev) { 
//在拦截处处理,在滑动设置了点击事件的地方也能swip,点击时又不能影响原来的点击事件 
int action = ev.getaction(); 
switch (action) { 
case motionevent.action_down: 
mdownx = ev.getx(); 
mdowny = ev.gety(); 
boolean handled = super.onintercepttouchevent(ev); 
mtouchstate = touch_state_none; 
mtouchposition = pointtoposition((int) ev.getx(), (int) ev.gety()); 
view view = getchildat(mtouchposition - getfirstvisibleposition()); 
//只在空的时候赋值 以免每次触摸都赋值,会有多个open状态 
if (view instanceof swipemenulayout) { 
//如果有打开了 就拦截. 
if (mtouchview != null && mtouchview.isopen() && !inrangeofview(mtouchview.getmenuview(), ev)) { 
return true; 
} 
mtouchview = (swipemenulayout) view; 
mtouchview.setswipedirection(mdirection); 
} 
//如果摸在另外个view 
if (mtouchview != null && mtouchview.isopen() && view != mtouchview) { 
handled = true; 
} 
if (mtouchview != null) { 
mtouchview.onswipe(ev); 
} 
return handled; 
case motionevent.action_move: 
float dy = math.abs((ev.gety() - mdowny)); 
float dx = math.abs((ev.getx() - mdownx)); 
if (math.abs(dy) > max_y || math.abs(dx) > max_x) { 
//每次拦截的down都把触摸状态设置成了touch_state_none 只有返回true才会走ontouchevent 所以写在这里就够了 
if (mtouchstate == touch_state_none) { 
if (math.abs(dy) > max_y) { 
mtouchstate = touch_state_y; 
} else if (dx > max_x) { 
mtouchstate = touch_state_x; 
if (monswipelistener != null) { 
monswipelistener.onswipestart(mtouchposition); 
} 
} 
} 
return true; 
} 
} 
return super.onintercepttouchevent(ev); 
} 
@override 
public boolean ontouchevent(motionevent ev) { 
if (ev.getaction() != motionevent.action_down && mtouchview == null) 
return super.ontouchevent(ev); 
int action = ev.getaction(); 
switch (action) { 
case motionevent.action_down: 
int oldpos = mtouchposition; 
mdownx = ev.getx(); 
mdowny = ev.gety(); 
mtouchstate = touch_state_none; 
mtouchposition = pointtoposition((int) ev.getx(), (int) ev.gety()); 
/*******把这句代码提前*********/ 
view view = getchildat(mtouchposition - getfirstvisibleposition()); 
if (mtouchposition == oldpos && mtouchview != null 
&& mtouchview.isopen()) { 
/********新添加的内容,当按下的item不是当前已经打开的item,则关闭已经打开的item,并返回false.不再响应down以后的事件,仿qq效果********/ 
if (view instanceof swipemenulayout) { 
swipemenulayout touchview = (swipemenulayout) view; 
if (!touchview.isopen()) { 
mtouchview.smoothclosemenu(); 
return false; 
} 
} 
/***************************/ 
mtouchstate = touch_state_x; 
mtouchview.onswipe(ev); 
return true; 
} 
if (mtouchview != null && mtouchview.isopen()) { 
mtouchview.smoothclosemenu(); 
mtouchview = null; 
// return super.ontouchevent(ev); 
// try to cancel the touch event 
motionevent cancelevent = motionevent.obtain(ev); 
cancelevent.setaction(motionevent.action_cancel); 
ontouchevent(cancelevent); 
if (monmenustatechangelistener != null) { 
monmenustatechangelistener.onmenuclose(oldpos); 
} 
return true; 
} 
if (view instanceof swipemenulayout) { 
mtouchview = (swipemenulayout) view; 
mtouchview.setswipedirection(mdirection); 
} 
if (mtouchview != null) { 
mtouchview.onswipe(ev); 
} 
break; 
case motionevent.action_move: 
//有些可能有header,要减去header再判断 
mtouchposition = pointtoposition((int) ev.getx(), (int) ev.gety()) - getheaderviewscount(); 
//如果滑动了一下没完全展现,就收回去,这时候mtouchview已经赋值,再滑动另外一个不可以swip的view 
//会导致mtouchview swip 。 所以要用位置判断是否滑动的是一个view 
if (!mtouchview.getswipenable() || mtouchposition != mtouchview.getposition()) { 
break; 
} 
float dy = math.abs((ev.gety() - mdowny)); 
float dx = math.abs((ev.getx() - mdownx)); 
if (mtouchstate == touch_state_x) { 
if (mtouchview != null) { 
mtouchview.onswipe(ev); 
} 
getselector().setstate(new int[]{0}); 
ev.setaction(motionevent.action_cancel); 
super.ontouchevent(ev); 
return true; 
} else if (mtouchstate == touch_state_none) { 
if (math.abs(dy) > max_y) { 
mtouchstate = touch_state_y; 
} else if (dx > max_x) { 
mtouchstate = touch_state_x; 
if (monswipelistener != null) { 
monswipelistener.onswipestart(mtouchposition); 
} 
} 
} 
break; 
case motionevent.action_up: 
if (mtouchstate == touch_state_x) { 
if (mtouchview != null) { 
boolean isbeforeopen = mtouchview.isopen(); 
mtouchview.onswipe(ev); 
boolean isafteropen = mtouchview.isopen(); 
if (isbeforeopen != isafteropen && monmenustatechangelistener != null) { 
if (isafteropen) { 
monmenustatechangelistener.onmenuopen(mtouchposition); 
} else { 
monmenustatechangelistener.onmenuclose(mtouchposition); 
} 
} 
if (!isafteropen) { 
mtouchposition = -1; 
mtouchview = null; 
} 
} 
if (monswipelistener != null) { 
monswipelistener.onswipeend(mtouchposition); 
} 
ev.setaction(motionevent.action_cancel); 
super.ontouchevent(ev); 
return true; 
} 
break; 
} 
return super.ontouchevent(ev); 
} 
public void smoothopenmenu(int position) { 
if (position >= getfirstvisibleposition() 
&& position <= getlastvisibleposition()) { 
view view = getchildat(position - getfirstvisibleposition()); 
if (view instanceof swipemenulayout) { 
mtouchposition = position; 
if (mtouchview != null && mtouchview.isopen()) { 
mtouchview.smoothclosemenu(); 
} 
mtouchview = (swipemenulayout) view; 
mtouchview.setswipedirection(mdirection); 
mtouchview.smoothopenmenu(); 
} 
} 
} 
public void smoothclosemenu() { 
if (mtouchview != null && mtouchview.isopen()) { 
mtouchview.smoothclosemenu(); 
} 
} 
private int dp2px(int dp) { 
return (int) typedvalue.applydimension(typedvalue.complex_unit_dip, dp, 
getcontext().getresources().getdisplaymetrics()); 
} 
public void setmenucreator(swipemenucreator menucreator) { 
this.mmenucreator = menucreator; 
} 
public void setonmenuitemclicklistener( 
onmenuitemclicklistener onmenuitemclicklistener) { 
this.monmenuitemclicklistener = onmenuitemclicklistener; 
} 
public void setonswipelistener(onswipelistener onswipelistener) { 
this.monswipelistener = onswipelistener; 
} 
public void setonmenustatechangelistener(onmenustatechangelistener onmenustatechangelistener) { 
monmenustatechangelistener = onmenustatechangelistener; 
} 
public static interface onmenuitemclicklistener { 
boolean onmenuitemclick(int position, swipemenu menu, int index); 
} 
public static interface onswipelistener { 
void onswipestart(int position); 
void onswipeend(int position); 
} 
public static interface onmenustatechangelistener { 
void onmenuopen(int position); 
void onmenuclose(int position); 
} 
public void setswipedirection(int direction) { 
mdirection = direction; 
} 
/** 
* 判断点击事件是否在某个view内 
* 
* @param view 
* @param ev 
* @return 
*/ 
public static boolean inrangeofview(view view, motionevent ev) { 
int[] location = new int[2]; 
view.getlocationonscreen(location); 
int x = location[0]; 
int y = location[1]; 
if (ev.getrawx() < x || ev.getrawx() > (x + view.getwidth()) || ev.getrawy() < y || ev.getrawy() > (y + view.getheight())) { 
return false; 
} 
return true; 
} 
}

这样看来实现qq的那个效果是不是很简单,不过也多亏了原作者写的好,我修改起来才更容易.

话说回来,当按下的item不是已经被拉出来的那个item时,相应的效果我们已经实现,如果按下的item正好是那个已经被拉出来的item,效果怎么实现呢?

其实对于这个效果原作者其实已经实现了,只不过实现的不够好,存在一些问题,我这里把这个问题修复了.

存在的问题就是:如果在当前被拉出的item上左右滑动时,当你在抬起手指的那一刻并且滑动方向是向着拉开的方向滑动,有很大几率,这个被拉开的item会被关闭,举个栗子,比如你设定的是向左滑动是拉开的方向,当你在已经被拉开的item上左右滑动,并且在抬起手指的前一刻你是向左滑动的,讲道理的话这个item应该是处于被拉开的状态,但是实际上有很大几率当你抬起手指时,这个item却被关闭了,所以这是一个相当影响用户体验问题,一个item的其实就是一个swipemenulayout,在swipemenulayout类中,有一个onswipe方法,该方法的参数motionevent就是从swipemenulistview的ontouchevent方法中传递过来的,之前说过,当手指抬起时才会出问题,那么我们直接就看action_up当中的实现代码,

以下代码是有问题的:

if ((isfling || math.abs(mdownx - event.getx()) > (mmenuview.getwidth() / 2)) && 
math.signum(mdownx - event.getx()) == mswipedirection) { 
// open 
smoothopenmenu(); 
} else { 
// close 
smoothclosemenu(); 
return false; 
} 

我们来分析一下,isfliing不用看,问题不是出在这里,我们看后面的判断逻辑:x轴方向上滑动距离的绝对值大于菜单宽度的一半并且滑动方向是向着拉开的方向滑动时,一个item才会被拉开,不然其他所有情况都会直接进else语句关闭item,问题就是出现在这里,而上面说的问题也是由这个导致的,试想一种情况,由于当前的item是已经被拉出来过的,如果我们的滑动距离绝对值没有超过菜单宽度的一半但是我们滑动的方向是向着item被拉出来的方向,讲道理的话我们的item是应该继续以被拉出的状态显示才对,但是根据代码中的判断逻辑,这种情况是直接进else语句的,也就是直接关闭这个item的,所以这里的逻辑判断有大问题,我们要的效果是:既然你已经被拉出来了只要你是继续往拉出的方向滑动,我就不会进else,而是直接再在if中进行判断滑动的距离,这样的逻辑才是正确的,而为了在第一次滑动时,不出现问题,我们还得为第一次滑动做相应操作.

所以修改之后的代码逻辑如下所示:

if ((isfling || math.signum(mdownx - event.getx()) == mswipedirection)) { 
// open 
/**************新添加内容****防止在已被拉开的item上向拉开的方向滑动然后抬起手指时,item有很大几率关闭的问题******************/ 
if (math.abs(mdownx - event.getx()) > (mmenuview.getwidth() / 2)) { 
smoothopenmenu(); 
} else { 
//没有item打开时,且滑动距离不满足打开的条件才进行关闭 
if (!isopen()) { 
smoothclosemenu(); 
} 
} 
/*******************************************/ 
} else { 
// close 
smoothclosemenu(); 
return false; 
} 

这样,我们就完美实现了qq的第二个效果,即如果你的手指放到当前被拉出的item上,他不会隐藏这个item,并且可以正常响应左右滑动事件.

完整的修改之后的swipemenulayout类的代码如下所示:

package com.lanma.swipemenulistviewdemo.swipemenulistview; 
import android.content.context; 
import android.support.v4.view.gesturedetectorcompat; 
import android.support.v4.widget.scrollercompat; 
import android.util.attributeset; 
import android.util.log; 
import android.util.typedvalue; 
import android.view.gesturedetector.ongesturelistener; 
import android.view.gesturedetector.simpleongesturelistener; 
import android.view.motionevent; 
import android.view.view; 
import android.view.animation.interpolator; 
import android.widget.abslistview; 
import android.widget.framelayout; 
/** 
* @author baoyz 
* @date 2014-8-23 
* qiang_xi修改于2016-09-07 
*/ 
public class swipemenulayout extends framelayout { 
private static final int content_view_id = 1; 
private static final int menu_view_id = 2; 
private static final int state_close = 0; 
private static final int state_open = 1; 
private int mswipedirection; 
private view mcontentview; 
private swipemenuview mmenuview; 
private int mdownx; 
private int state = state_close; 
private gesturedetectorcompat mgesturedetector; 
private ongesturelistener mgesturelistener; 
private boolean isfling; 
private int min_fling = dp2px(15); 
private int max_velocityx = -dp2px(500); 
private scrollercompat mopenscroller; 
private scrollercompat mclosescroller; 
private int mbasex; 
private int position; 
private interpolator mcloseinterpolator; 
private interpolator mopeninterpolator; 
private boolean mswipenable = true; 
public swipemenulayout(view contentview, swipemenuview menuview) { 
this(contentview, menuview, null, null); 
} 
public swipemenulayout(view contentview, swipemenuview menuview, 
interpolator closeinterpolator, interpolator openinterpolator) { 
super(contentview.getcontext()); 
mcloseinterpolator = closeinterpolator; 
mopeninterpolator = openinterpolator; 
mcontentview = contentview; 
mmenuview = menuview; 
mmenuview.setlayout(this); 
init(); 
} 
// private swipemenulayout(context context, attributeset attrs, int 
// defstyle) { 
// super(context, attrs, defstyle); 
// } 
private swipemenulayout(context context, attributeset attrs) { 
super(context, attrs); 
} 
private swipemenulayout(context context) { 
super(context); 
} 
public int getposition() { 
return position; 
} 
public void setposition(int position) { 
this.position = position; 
mmenuview.setposition(position); 
} 
public void setswipedirection(int swipedirection) { 
mswipedirection = swipedirection; 
} 
private void init() { 
setlayoutparams(new abslistview.layoutparams(layoutparams.match_parent, 
layoutparams.wrap_content)); 
mgesturelistener = new simpleongesturelistener() { 
@override 
public boolean ondown(motionevent e) { 
isfling = false; 
return true; 
} 
@override 
public boolean onfling(motionevent e1, motionevent e2, 
float velocityx, float velocityy) { 
// todo 
if (math.abs(e1.getx() - e2.getx()) > min_fling 
&& velocityx < max_velocityx) { 
isfling = true; 
} 
// log.i("byz", max_velocityx + ", velocityx = " + velocityx); 
return super.onfling(e1, e2, velocityx, velocityy); 
} 
}; 
mgesturedetector = new gesturedetectorcompat(getcontext(), 
mgesturelistener); 
// mscroller = scrollercompat.create(getcontext(), new 
// bounceinterpolator()); 
if (mcloseinterpolator != null) { 
mclosescroller = scrollercompat.create(getcontext(), 
mcloseinterpolator); 
} else { 
mclosescroller = scrollercompat.create(getcontext()); 
} 
if (mopeninterpolator != null) { 
mopenscroller = scrollercompat.create(getcontext(), 
mopeninterpolator); 
} else { 
mopenscroller = scrollercompat.create(getcontext()); 
} 
layoutparams contentparams = new layoutparams( 
layoutparams.match_parent, layoutparams.wrap_content); 
mcontentview.setlayoutparams(contentparams); 
if (mcontentview.getid() < 1) { 
mcontentview.setid(content_view_id); 
} 
mmenuview.setid(menu_view_id); 
mmenuview.setlayoutparams(new layoutparams(layoutparams.wrap_content, 
layoutparams.wrap_content)); 
addview(mcontentview); 
addview(mmenuview); 
// if (mcontentview.getbackground() == null) { 
// mcontentview.setbackgroundcolor(color.white); 
// } 
// in android 2.x, menuview height is match_parent is not work. 
// getviewtreeobserver().addongloballayoutlistener( 
// new ongloballayoutlistener() { 
// @override 
// public void ongloballayout() { 
// setmenuheight(mcontentview.getheight()); 
// // getviewtreeobserver() 
// // .removeglobalonlayoutlistener(this); 
// } 
// }); 
} 
@override 
protected void onattachedtowindow() { 
super.onattachedtowindow(); 
} 
@override 
protected void onsizechanged(int w, int h, int oldw, int oldh) { 
super.onsizechanged(w, h, oldw, oldh); 
} 
public boolean onswipe(motionevent event) { 
mgesturedetector.ontouchevent(event); 
switch (event.getaction()) { 
case motionevent.action_down: 
mdownx = (int) event.getx(); 
isfling = false; 
break; 
case motionevent.action_move: 
// log.i("byz", "downx = " + mdownx + ", movex = " + event.getx()); 
int dis = (int) (mdownx - event.getx()); 
if (state == state_open) { 
dis += mmenuview.getwidth() * mswipedirection; 
} 
swipe(dis); 
break; 
case motionevent.action_up: 
if ((isfling || math.signum(mdownx - event.getx()) == mswipedirection)) { 
// open 
/**************新添加内容****防止在已被拉开的item上向拉开的方向滑动然后抬起手指时,item有很大几率关闭的问题******************/ 
if (math.abs(mdownx - event.getx()) > (mmenuview.getwidth() / 2)) { 
smoothopenmenu(); 
} else { 
//没有item打开时,且滑动距离不满足打开的条件才进行关闭 
if (!isopen()) { 
smoothclosemenu(); 
} 
} 
/*******************************************/ 
} else { 
// close 
smoothclosemenu(); 
return false; 
} 
break; 
} 
return true; 
} 
public boolean isopen() { 
return state == state_open; 
} 
@override 
public boolean ontouchevent(motionevent event) { 
return super.ontouchevent(event); 
} 
private void swipe(int dis) { 
if (!mswipenable) { 
return; 
} 
if (math.signum(dis) != mswipedirection) { 
dis = 0; 
} else if (math.abs(dis) > mmenuview.getwidth()) { 
dis = mmenuview.getwidth() * mswipedirection; 
} 
mcontentview.layout(-dis, mcontentview.gettop(), 
mcontentview.getwidth() - dis, getmeasuredheight()); 
if (mswipedirection == swipemenulistview.direction_left) { 
mmenuview.layout(mcontentview.getwidth() - dis, mmenuview.gettop(), 
mcontentview.getwidth() + mmenuview.getwidth() - dis, 
mmenuview.getbottom()); 
} else { 
mmenuview.layout(-mmenuview.getwidth() - dis, mmenuview.gettop(), 
-dis, mmenuview.getbottom()); 
} 
} 
@override 
public void computescroll() { 
if (state == state_open) { 
if (mopenscroller.computescrolloffset()) { 
swipe(mopenscroller.getcurrx() * mswipedirection); 
postinvalidate(); 
} 
} else { 
if (mclosescroller.computescrolloffset()) { 
swipe((mbasex - mclosescroller.getcurrx()) * mswipedirection); 
postinvalidate(); 
} 
} 
} 
public void smoothclosemenu() { 
state = state_close; 
if (mswipedirection == swipemenulistview.direction_left) { 
mbasex = -mcontentview.getleft(); 
mclosescroller.startscroll(0, 0, mmenuview.getwidth(), 0, 350); 
} else { 
mbasex = mmenuview.getright(); 
mclosescroller.startscroll(0, 0, mmenuview.getwidth(), 0, 350); 
} 
postinvalidate(); 
} 
public void smoothopenmenu() { 
if (!mswipenable) { 
return; 
} 
state = state_open; 
if (mswipedirection == swipemenulistview.direction_left) { 
mopenscroller.startscroll(-mcontentview.getleft(), 0, mmenuview.getwidth(), 0, 350); 
} else { 
mopenscroller.startscroll(mcontentview.getleft(), 0, mmenuview.getwidth(), 0, 350); 
} 
postinvalidate(); 
} 
public void closemenu() { 
if (mclosescroller.computescrolloffset()) { 
mclosescroller.abortanimation(); 
} 
if (state == state_open) { 
state = state_close; 
swipe(0); 
} 
} 
public void openmenu() { 
if (!mswipenable) { 
return; 
} 
if (state == state_close) { 
state = state_open; 
swipe(mmenuview.getwidth() * mswipedirection); 
} 
} 
public view getcontentview() { 
return mcontentview; 
} 
public swipemenuview getmenuview() { 
return mmenuview; 
} 
private int dp2px(int dp) { 
return (int) typedvalue.applydimension(typedvalue.complex_unit_dip, dp, 
getcontext().getresources().getdisplaymetrics()); 
} 
@override 
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { 
super.onmeasure(widthmeasurespec, heightmeasurespec); 
mmenuview.measure(measurespec.makemeasurespec(0, 
measurespec.unspecified), measurespec.makemeasurespec( 
getmeasuredheight(), measurespec.exactly)); 
} 
@override 
protected void onlayout(boolean changed, int l, int t, int r, int b) { 
mcontentview.layout(0, 0, getmeasuredwidth(), 
mcontentview.getmeasuredheight()); 
if (mswipedirection == swipemenulistview.direction_left) { 
mmenuview.layout(getmeasuredwidth(), 0, 
getmeasuredwidth() + mmenuview.getmeasuredwidth(), 
mcontentview.getmeasuredheight()); 
} else { 
mmenuview.layout(-mmenuview.getmeasuredwidth(), 0, 
0, mcontentview.getmeasuredheight()); 
} 
} 
public void setmenuheight(int measuredheight) { 
log.i("byz", "pos = " + position + ", height = " + measuredheight); 
layoutparams params = (layoutparams) mmenuview.getlayoutparams(); 
if (params.height != measuredheight) { 
params.height = measuredheight; 
mmenuview.setlayoutparams(mmenuview.getlayoutparams()); 
} 
} 
public void setswipenable(boolean swipenable) { 
mswipenable = swipenable; 
} 
public boolean getswipenable() { 
return mswipenable; 
} 
} 

那么.由于我们的标题名字起的是99.99%实现侧滑删除效果,那么我们就来看下效果图,看看到底是不是99.99%的一样:

qq效果图:

Android自定义view系列之99.99%实现QQ侧滑删除效果实例代码详解 

修改后的效果图:

Android自定义view系列之99.99%实现QQ侧滑删除效果实例代码详解

上面的两张效果图可能看不出什么,特别是我们上面说的那几个效果,主要是视屏转成gif之后又卡播放速度又快,所以想比较qq的效果和我们自己的效果,还是下载demo自己比较吧.我只能说改过之后的效果和qq的效果基本就是一样的.嘎嘎~~