用苹果的小伙伴们都会知道,ios系统退出当前的页面只需要向右边侧滑,这是一个非常好的用户体验,但是Android官方并没有出具有这有性质的Activity,所以我们只能辛苦辛苦,自己动手写一个具有这种性质的Activity。
其实写这个自定义的Activity还是非常简单的。我们主要用到一个这个控件SlidingPaneLayout,使用这个控件来模拟我们的这种效果的好处是我们基本不用去管一些手势事件冲突问题,SlidingPaneLayout已经帮我们做好了;
public abstract class BaseActivity extends AppCompatActivity implements SlidingPaneLayout.PanelSlideListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
initSwipeBackFinish();
super.onCreate(savedInstanceState);
}
/**
* 初始化滑动返回
*/
private void initSwipeBackFinish() {
if (isSupportSwipeBack()) {
SlidingPaneLayout slidingPaneLayout = new SlidingPaneLayout(this);
//通过反射改变mOverhangSize的值为0,这个mOverhangSize值为菜单到右边屏幕的最短距离,
//默认是32dp
try {
//更改属性
Field field = SlidingPaneLayout.class.getDeclaredField("mOverhangSize");
field.setAccessible(true);
field.set(slidingPaneLayout, 0);
} catch (Exception e) {
e.printStackTrace();
}
//设置监听事件
slidingPaneLayout.setPanelSlideListener(this);
slidingPaneLayout.setSliderFadeColor(getResources().getColor(android.R.color.transparent));
View leftView = new View(this);
leftView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//在左边添加这个视图
slidingPaneLayout.addView(leftView, 0);
//获取到最顶层的视图容器
ViewGroup decor = (ViewGroup) getWindow().getDecorView();
//获取到左边的视图
ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
//设置左边的视图为透明 decorChild.setBackgroundColor(getResources().getColor(android.R.color.transparent));
decor.removeView(decorChild);
decor.addView(slidingPaneLayout);
//在右边添加这个视图
slidingPaneLayout.addView(decorChild, 1);
}
}
/**
* 是否支持滑动退出
*/
protected boolean isSupportSwipeBack() {
return true;
}
@Override
public void onPanelClosed(View view) {
}
@Override
public void onPanelOpened(View view) {
finish();
//设置Activity退出的动画
this.overridePendingTransition(0, R.anim.slide_out_right);
}
@Override
public void onPanelSlide(View view, float v) {
}
}
来源:CSDN
作者:superQad
链接:https://blog.csdn.net/java_android_js/article/details/50822337