首先加入开源库
compile 'me.imid.swipebacklayout.lib:library:1.0.0'
使当前的Activity继承SwipeBackActivity
public class BaseActivity extends SwipeBackActivity{
privateSwipeBackLayoutmSwipeBackLayout;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);mSwipeBackLayout=getSwipeBackLayout();//设置滑动方向,可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOMmSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);}}
在使用的主题中添加下面的属性,否则滑动时activity的下层是黑色的
android:windowIsTranslucent true
当使用BaseActivity时,为了使首页不会滑动删除,只需如下设置即可
setSwipeBackEnable(false);//禁止滑动删除
另外,这个滑动删除的效果只能从边界滑动才有效果,如果要扩大touch的范围,可以调用
mSwipeBackLayout.setEdgeSize(intsize);
但是上面这个方法不太好用,效果不明显,推荐修改ViewDragHelper.java这个类源码中的getEdgeTouched(int x, int y)方法,如下
privateintgetEdgeTouched(intx,inty){intresult=0;result=EDGE_LEFT;//这样每次都是全屏左滑删除//解决只有点击屏幕左边才有响应的问题/*if (x < mParentView.getLeft() + mEdgeSize)
result = EDGE_LEFT;
if (y < mParentView.getTop() + mEdgeSize)
result = EDGE_TOP;
if (x > mParentView.getRight() - mEdgeSize)
result = EDGE_RIGHT;
if (y > mParentView.getBottom() - mEdgeSize)
result = EDGE_BOTTOM;*/returnresult;
}