需求描述:在应用中界面,点击或双击状态栏区域,返回页面顶部;模仿ios的效果
实现方案:模拟事件下发
代码实现:
PhoneStatusBarView.java
public boolean onTouchEvent(MotionEvent event) {
中增加代码:
doScrollBack(event);
============================================
增加以下函数:
private void doScrollBack(MotionEvent event) {
final int action = event.getAction();
final int rawX = (int) event.getRawX();
/*
* 1、判断是否点击返回区域
*/
if (action == MotionEvent.ACTION_DOWN) {
/*
* 回滚顶部点击除时间和电池以外的区域都有效
*/
mScrollTriggered = (rawX > 0.25 * getWidth() && rawX < 0.75 * getWidth()) ? true : false;
}
/*
* 2、下拉取消回滚操作
*/
if (mPanel.getExpandedHeight() > 100) {
mScrollTriggered = false;
}
/*
* 3、松手后执行回滚操作
*/
if ((action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL)) {
if (mScrollTriggered) {
goScrollBack();
}
}
}
public void doScrollBack() {
//对ListView GridView处理
new Handler().postDelayed(doScrollBackRun_ListView, 150);
//对ScrollView处理
new Handler().postDelayed(doScrollBackRun_ScrollView, 150);
}
Runnable doScrollBackRun_ListView = new Runnable() {
public void run() {
long now = SystemClock.uptimeMillis();
final KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_MOVE_HOME, 0, 0);
final KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_MOVE_HOME, 0, 0);
InputManager.getInstance().injectInputEvent(down,
InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
InputManager.getInstance().injectInputEvent(up,
InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
}
};
Runnable doScrollBackRun_ScrollView = new Runnable() {
public void run() {
long now = SystemClock.uptimeMillis();
final KeyEvent down = new KeyEvent(now, now, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DPAD_UP, 0, 0);
final KeyEvent up = new KeyEvent(now, now, KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_DPAD_UP, 0, 0);
InputManager.getInstance().injectInputEvent(down,
InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
InputManager.getInstance().injectInputEvent(up,
InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
}
};