添加依赖:
implementation 'me.imid.swipebacklayout.lib:library:1.3.0'
使用代码:
import nsu.edu.com.library.SwipeBackActivity;
import nsu.edu.com.library.SwipeBackLayout; public class DemoActivity extends SwipeBackActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
// 可以调用该方法,设置是否允许滑动退出
setSwipeBackEnable(true);
SwipeBackLayout mSwipeBackLayout = getSwipeBackLayout();
// 设置滑动方向,可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM
mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
// 滑动退出的效果只能从边界滑动才有效果,如果要扩大touch的范围,可以调用这个方法
//mSwipeBackLayout.setEdgeSize(200);
}
}
具体源码分析可以参考https://www.jianshu.com/p/92b884d8471c