在android开发中,往往有这样的需求,整个app中,实现手势从左往右滑动一定距离,整个界面销毁回到上一级。但是有个问题,就是在viewpager等滑动切换界面的时候,往往会存在一个兼容性的问题,那么如何解决呢?其实我的实现方式很简单,就是采用事件分发机制来实现!
/** * @类名:BaseActivity * @类描述:activity基类,所有子类需继承此类 * @作者:Administrator */
public abstract class BaseActivity extends Activity {
// 手指向右滑动时的最小距离
private int XDISTANCE_MIN = 440;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
EventBus.getDefault().register(this);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
XDISTANCE_MIN = Utils.getScreenWidth(this) * 7 / 10;
onPreOnCreate(savedInstanceState);
}
/** * @方法说明:启动指定activity * @方法名称:openActivity * @param pClass * @返回void */
public void openActivity(Class<?> pClass) {
openActivity(pClass, null);
}
/** * @方法说明:启动到指定activity,Bundle传递对象(作用个界面之间传递数据) * @方法名称:openActivity * @param pClass * @param pBundle * @返回void */
public void openActivity(Class<?> pClass, Bundle pBundle) {
Intent intent = new Intent(this, pClass);
if (pBundle != null) {
intent.putExtras(pBundle);
}
startActivity(intent);
}
/** * @方法说明:根据界面name启动到指定界 * @方法名称:openActivity * @param pAction * @返回void */
public void openActivity(String pAction) {
openActivity(pAction, null);
}
/** * @方法说明:根据界面name启动到指定界面,Bundle传递对象(作用个界面之间传递数据) * @方法名称:openActivity * @param pAction * @param pBundle * @返回void */
public void openActivity(String pAction, Bundle pBundle) {
Intent intent = new Intent(pAction);
if (pBundle != null) {
intent.putExtras(pBundle);
}
startActivity(intent);
}
/** * @方法说明:A-Activity需要在B-Activtiy中执行一些数据操作,而B-Activity又要将,执行操作数据的结果返回给A-Activity * @方法名称:openActivityResult * @param pClass * @返回void */
public void openActivityResult(Class<?> pClass) {
openActivityResult(pClass, null);
}
/** * @方法说明:A-Activity需要在B-Activtiy中执行一些数据操作, * 而B-Activity又要将,执行操作数据的结果返回给A-Activity * Bundle传递对象(作用个界面之间传递数据) * @方法名称:openActivityResult * @param pClass * @param pBundle * @返回void */
public void openActivityResult(Class<?> pClass, Bundle pBundle) {
openActivityResult(pClass, pBundle, 0);
}
/** * @方法说明:A-Activity需要在B-Activtiy中执行一些数据操作, * 而B-Activity又要将,执行操作数据的结果返回给A-Activity * Bundle传递对象(作用个界面之间传递数据) * @方法名称:openActivityResult * @param pClass * @param pBundle * @param requestCode * @返回void */
public void openActivityResult(Class<?> pClass, Bundle pBundle,
int requestCode) {
Intent intent = new Intent(this, pClass);
if (pBundle != null) {
intent.putExtras(pBundle);
}
startActivityForResult(intent, requestCode);
}
/* * @重写方法 onKeyDown * * @父类:@see android.app.Activity#onKeyDown(int, android.view.KeyEvent) * * @方法说明: */
@SuppressWarnings("static-access")
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == event.KEYCODE_BACK) {
if (Utils.backDouble(800)) {
return super.onKeyDown(keyCode, event);
}
finishBase();
}
return super.onKeyDown(keyCode, event);
}
/** * @方法说明:退出栈中所有Activity * @方法名称:finishBase * @返回void */
public void finishBase() {
LynActivityManager.getScreenManager().popAllActivityExceptOne(
getClass());
finish();
}
/** * @方法说明:退出应用程 * @方法名称:AppExit * @param context * 上下 * @param isBackground是否开开启后台运 * @返回值:void * @param isBackground是否开开启后台运 * * @返回void */
@SuppressWarnings("deprecation")
public void AppExit(Context context, Boolean isBackground) {
try {
finishBase();
ActivityManager activityMgr = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.restartPackage(context.getPackageName());
} catch (Exception e) {
} finally {
// 注意,如果您有后台程序运行,请不要支持此句子
if (!isBackground) {
System.exit(0);
}
}
}
/** * @重写方法onDestroy * @父类:@see android.app.Activity#onDestroy() * @方法说明: */
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
// 销毁当前的activity
LynActivityManager.getScreenManager().popActivity(this);
// removeProgress();
releaseMemory();
LynActivityManager.getScreenManager().popActivity(this);
}
/** * @方法说明:初始化界 * @方法名称:onPreOnCreate * @param savedInstanceState * @返回void */
public abstract void onPreOnCreate(Bundle savedInstanceState);
/** * @方法说明:手动释放内存 * @方法名称:releaseMemory * @返回void */
public abstract void releaseMemory();
public void refresh(Object... param) {
};
/** * @方法说明:有网络处理 * @方法名称:onConnect * @param type * @返回void */
public void onConnect(netType type) {
// Toast.makeText(this, "当前网络是:" + type.toString(),
// Toast.LENGTH_SHORT).show();
}
/** * @方法说明:无网络处理 * @方法名称:onDisConnect * @返回void */
public void onDisConnect() {
Toast.makeText(this, "当前无网络", Toast.LENGTH_SHORT).show();
}
// 手指向右滑动时的最小速度
private static final int XSPEED_MIN = 2000;
// 记录手指按下时的横坐标。
private float xDown;
// 记录手指移动时的横坐标。
private float xMove;
// 用于计算手指滑动的速度。
private VelocityTracker mVelocityTracker;
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
createVelocityTracker(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
xDown = event.getRawX();
break;
case MotionEvent.ACTION_MOVE:
xMove = event.getRawX();
// 活动的距离
int distanceX = (int) (xMove - xDown);
// 获取顺时速度
int xSpeed = getScrollVelocity();
// 当滑动的距离大于我们设定的最小距离且滑动的瞬间速度大于我们设定的速度时,返回到上一个activity
if (distanceX > XDISTANCE_MIN && xSpeed > XSPEED_MIN) {
finish();
// 设置切换动画,从右边进入,左边退出
overridePendingTransition(R.anim.in_from_left,
R.anim.out_from_right);
}
break;
case MotionEvent.ACTION_UP:
recycleVelocityTracker();
break;
default:
break;
}
return super.dispatchTouchEvent(event);
}
/** * 创建VelocityTracker对象,并将触摸content界面的滑动事件加入到VelocityTracker当中。 * * @param event * */
private void createVelocityTracker(MotionEvent event) {
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
}
/** * 回收VelocityTracker对象。 */
private void recycleVelocityTracker() {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
/** * 获取手指在content界面滑动的速度。 * * @return 滑动速度,以每秒钟移动了多少像素值为单位。 */
private int getScrollVelocity() {
mVelocityTracker.computeCurrentVelocity(1000);
int velocity = (int) mVelocityTracker.getXVelocity();
return Math.abs(velocity);
}
}
好吧,相信这个还是很简单的!