Activity中的滑动监听事件

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;

public abstract class BaseSetupActivity extends Activity {

	protected static final String TAG = "BaseSetupActivity";
	private GestureDetector mDetector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 创建
		mDetector = new GestureDetector(this, new SimpleOnGestureListener() {
			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {

				// e1: 开始的点的数据
				// e2: 结束的点的数据
				// velocityX:
				// velocityY:

				float x1 = e1.getX();
				float x2 = e2.getX();

				float y1 = e1.getY();
				float y2 = e2.getY();
				Logger.d(TAG, "velocityX : " + velocityX);
				// 速率的过滤
				if (Math.abs(velocityX) < 80) {
					return false;
				}

				// 如果用户是垂直方向滑动不做操作
				if (Math.abs(y1 - y2) > Math.abs(x1 - x2)) {
					return false;
				}

				// 如果从右往左滑动(x1 > x2),下一步操作
				if (x1 > x2) {
					// 下一步操作
					doNext();
				} else {
					// 如果从左往右滑动,上一步操作
					doPre();
				}
				return super.onFling(e1, e2, velocityX, velocityY);
			}
		});
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mDetector.onTouchEvent(event);
		return super.onTouchEvent(event);
	}

	public void clickPre(View view) {
		doPre();
	}

	private void doPre() {
		// 页面跳转 (不同点)--->交给具体的子类实现
		// Intent intent = new Intent(this, SjfdSetup1Activity.class);
		// startActivity(intent);
		if (performPre()) {
			// 流程中断
			return;
		}

		// enterAnim: 进入的activity的动画
		// exitAnim: 退出的activity的动画
		overridePendingTransition(R.anim.pre_enter, R.anim.pre_exit);

		finish();
	}

	public void clickNext(View view) {
		doNext();
	}

	private void doNext() {
		// 页面跳转 (不同点)--->交给具体的子类实现
		// Intent intent = new Intent(this, SjfdSetup3Activity.class);
		// startActivity(intent);
		if (performNext()) {
			return;
		}

		// (共同点) --->父类实现
		// 动画
		// enterAnim: 进入的activity的动画
		// exitAnim: 退出的activity的动画
		overridePendingTransition(R.anim.next_enter, R.anim.next_exit);

		// 销毁
		finish();
	}

	/**
	 * 执行上一步的操作
	 * 
	 * @return 如果返回true 中断流程
	 */
	protected abstract boolean performPre();

	/**
	 * 执行下一步的操作
	 * 
	 * @return 如果返回true 中断流程
	 */
	protected abstract boolean performNext();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值