标签:android
在知乎客户端上看到了这种效果,左滑Activity可以返回到上一界面,非常适合单手操作。
找了很久,终于在github上看到了SwipeBackLayout这个开源项目,地址:
需要使用到的类:
SwipeBackActivity.java
SwipeBackLayout.java
ViewDragHelper.java
public class BaseActivity extends SwipeBackActivity {
private SwipeBackLayout mSwipeBackLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSwipeBackLayout = getSwipeBackLayout();
//设置滑动方向,可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM
mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
}
}
<item name="android:windowIsTranslucent">true</item>
setSwipeBackEnable(false); //禁止滑动删除
mSwipeBackLayout.setEdgeSize(int size);
但是上面这个方法不太好用,效果不明显,推荐修改ViewDragHelper.java这个类源码中的getEdgeTouched(int x, int y)方法,如下
private int getEdgeTouched(int x, int y) {
int result = 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;*/
return result;
}
经过上面步骤,应该就能如期实现效果了。
下面放上我的Demo截图:
注:Demo源码稍后会放上来。
标签:android
原文地址:http://blog.csdn.net/eiuly/article/details/46472783