继上篇文章Android 利用ViewDragHelper打造侧滑关闭控件,因为上次有事出门了,没有扩展这个功能,今天刚好周末有点时间,索性补上吧。
效果展示:
思路梳理:执行当前页面侧滑关闭的时候,需要上个页面同步滚动,从而达到联动的效果,联动效果很简单,使用属性动画即可,问题是怎么拿到上个页面的View?我们知道Activity可以拿到当前页面的View,所以我们需要拿到上个页面的Activity,那么有什么好的做法呢?这里提供两种方法:
-
1.在基类BaseActivity中将自己的实例保存下来。
-
2.使用Application提供的ActivityLifecycleCallbacks,在Activity创建时将实例保存。
我这里使用第二种方法:
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import java.util.Stack;
/**
* Created by Chao on 2018-09-15.
*/
public class SwipeBackManager implements Application.ActivityLifecycleCallbacks {
private Stack<Activity> mActivityStack = new Stack<>();
private static final SwipeBackManager instance = new SwipeBackManager();
private SwipeBackManager() {
}
public static SwipeBackManager getInstance() {
return instance;
}
/**
* 必须在 Application 的 onCreate 方法中调用
*/
public void init(Application application) {
application.registerActivityLifecycleCallbacks(this);
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
//Activity创建时,将其保存。
mActivityStack.add(activity);
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
//Activity销毁时,将其移除。
mActivityStack.remove(activity);
}
/**
* 获取倒数第二个 Activity
*/
@Nullable
private Activity getPenultimateActivity() {
Activity activity = null;
try {
if (mActivityStack.size() > 1) {
activity = mActivityStack.get(mActivityStack.size() - 2);
}
} catch (Exception e) {
}
return activity;
}
/**
* 暴露Activity滑动动画
*/
public void onPanelSlide(float slideOffset) {
try {
Activity activity = getPenultimateActivity();
if (activity != null) {
View decorView = activity.getWindow().getDecorView();
decorView.setTranslationX(-(decorView.getMeasuredWidth() - slideOffset) / 3);
}
} catch (Exception e) {
}
}
/**
* 暴露Activity关闭动画
*/
public void onPanelClosed() {
try {
Activity activity = getPenultimateActivity();
if (activity != null) {
View decorView = activity.getWindow().getDecorView();
decorView.setTranslationX(0);
}
} catch (Exception e) {
}
}
}
然后在Application中注册:
import android.app.Application;
import view.peakchao.view.view.SwipeBackManager;
/**
* Created by Chao on 2018-09-15.
*/
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
SwipeBackManager.getInstance().init(this);
}
}