日志表现出:startFling()方法先后在onFling()和onTouch()调用。按照逻辑来说,在onFling()中调用了,即使在onTouch()方法中调用也不会再执行。
11-01 16:09:55.147: D/WatchTvFragment(1026): ++programTouchListener.onTouch++11-01 16:09:55.147: D/WatchTvFragment(1026): ++onFling++11-01 16:09:55.147: D/WatchTvFragment(1026): velocityX:139.54526, velocityY:225.460211-01 16:09:55.147: D/WatchTvFragment(1026): onFling() set bFlinging to true11-01 16:09:55.147: D/WatchTvFragment(1026): onFling() invode startFling()11-01 16:09:55.147: D/WatchTvFragment(1026): ++startFling++11-01 16:09:55.147: D/WatchTvFragment(1026): event.getAction():111-01 16:09:55.147: D/WatchTvFragment(1026): onTouch() set bFlinging to true11-01 16:09:55.147: D/WatchTvFragment(1026): onTouch() invode startFling()11-01 16:09:55.147: D/WatchTvFragment(1026): ++startFling++11-01 16:09:55.197: D/WatchTvFragment(1026): run() set bFlinging to false11-01 16:09:55.247: D/WatchTvFragment(1026): run() set bFlinging to false
经过处理,startFling()方法不会既执行onFling()方法的调用又去执行onTouch()方法的调用。
在onTouch()方法中的相关代码片段如下
case MotionEvent.ACTION_UP:Logger.i(TAG, "event.getAction():" + MotionEvent.ACTION_UP);programWidth = programListFragment.getView().getMeasuredWidth();float velocityX = (programParams.leftMargin < programWidth / 2) ? -1000 : 1000;Logger.i(TAG, "onTouch() invode startFling()");startFling(velocityX);break;
在onFling()方法中的相关代码片段如下
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Logger.i(TAG, "++onFling++"); Logger.i(TAG, "velocityX:" + velocityX + ", velocityY:" + velocityY); Logger.i(TAG, "onFling() invode startFling()"); startFling(velocityX); return true; // 需要返回true}
方法startFling()的代码片段如下
boolean bFlinging = false;void stopFling() { bFlinging = false;}synchronized void startFling(final float velocityX) { Logger.i(TAG, "++startFling++"); if (bFlinging) { Logger.i(TAG, "invalid operation."); return; } Logger.i(TAG, "flinging..."); bFlinging = true; programWidth = programListFragment.getView().getMeasuredWidth(); new Thread() { public void run() { int interval = 50; float xDistFling; int newLeftMargin; float decay = 0.98f; float v; // 速度从(+/-)1000px/s开始。 if (velocityX > 0 && velocityX < 1000) v = 1000; else if (velocityX < 0 && velocityX > -1000) v = -1000; else v = velocityX; // running表示while循环什么时候结束 boolean running = true; while (running) { v *= decay; xDistFling = v / 1000 * interval; newLeftMargin = programParams.leftMargin + (int) xDistFling; if (newLeftMargin <= 0) { newLeftMargin = 0; running = false; } else if (newLeftMargin >= channelWidth) { newLeftMargin = channelWidth; running = false; } programParams.leftMargin = newLeftMargin; try { Thread.sleep(interval); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(0); } Logger.i(TAG, "flinging finished."); } }.start();}
打印出来的日志表现是符合逻辑的,日志如下。
(1)onFling()中执行startFling()方法,在onTouch()中再执行被视为无效操作。
11-01 16:30:29.427: D/WatchTvFragment(2654): ++programTouchListener.onTouch++11-01 16:30:29.427: D/WatchTvFragment(2654): ++onFling++11-01 16:30:29.427: D/WatchTvFragment(2654): velocityX:-348.25256, velocityY:215.8674811-01 16:30:29.427: D/WatchTvFragment(2654): onFling() invode startFling()11-01 16:30:29.427: D/WatchTvFragment(2654): ++startFling++11-01 16:30:29.427: D/WatchTvFragment(2654): flinging...11-01 16:30:29.427: D/WatchTvFragment(2654): event.getAction():111-01 16:30:29.427: D/WatchTvFragment(2654): onTouch() invode startFling()11-01 16:30:29.427: D/WatchTvFragment(2654): ++startFling++11-01 16:30:29.427: D/WatchTvFragment(2654): invalid operation.11-01 16:30:29.987: D/WatchTvFragment(2654): flinging finished.
(2)onFling()中没有执行startFling()方法,在onTouch()中可以执行。
11-01 17:30:52.257: D/WatchTvFragment(8850): ++programTouchListener.onTouch++11-01 17:30:52.257: D/WatchTvFragment(8850): event.getAction():111-01 17:30:52.257: D/WatchTvFragment(8850): onTouch() invode startFling()11-01 17:30:52.257: D/WatchTvFragment(8850): ++startFling++11-01 17:30:52.257: D/WatchTvFragment(8850): flinging...11-01 17:30:52.307: D/WatchTvFragment(8850): flinging finished.