使用github上的SwipeBackLayout这个开源项目:
https://github.com/ikew0ng/SwipeBackLayout
现在项目中导入SwipeBackLayout模块,或者添加在gradle中添加依赖:
compile 'me.imid.swipebacklayout.lib:library:1.0.0'
定义基类BaseActivity继承SwipeBackActivity:
publicclassBaseActivityextends SwipeBackActivity {
private SwipeBackLayout mSwipeBackLayout;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSwipeBackLayout = getSwipeBackLayout();
//设置滑动方向,可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM
mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
}
}
需要有右滑返回的Activity只要继承这个BaseActivity即可。
主页禁止右滑返回需设置:
publicclassMainActivityextends BaseActivity {
private SwipeBackLayout mSwipeBackLayout;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSwipeBackLayout = getSwipeBackLayout();
//设置滑动方向,可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM
mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
setSwipeBackEnable(false);
//设置滑动的距离
mSwipeBackLayout.setEdgeSize(400);
}
}
现在可以右滑返回了,但是发现滑动时activity的下层是黑色的,在使用的主题中添加下面的属性:
<item name="android:windowIsTranslucent">false</item>
添加到application下面的style中。
解决SwipeBackLayout滑动返回时显示桌面而不是显示上一层的Bug:
在那个Activity的style下添加设置即可
<item name="android:windowIsTranslucent">false</item>
SwipeBackLayout源代码分析参看:
http://skykai521.github.io/2016/03/04/SwipeBackLayout%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%90/