2016-04-22 157 views
0

我有兩項活動:ActivityA & ActivityBActivityA正在偵聽運動事件。滾動事件發生時,立即啓動ActivityB繼續從一個活動滾動到另一個活動

ActivityB裏面有一個可滾動的內容,我希望它立即開始滾動此內容,而不會強迫用戶舉起手指。過渡必須是透明的,滾動手勢不應該停止在活動之間,而是順利地繼續。

換句話說,問題在於ActivityB對運動事件沒有反應,除非用戶擡起手指並再次觸摸屏幕。

我該如何解決這個問題?

+0

您不能將最後一個MotionEvent信息從A傳遞給B,然後模擬一個'一旦B啓動,ACTION_DOWN'? – NSimon

+0

@NicolasSimon我認爲這是正確的方向,我一直在探索它,迄今爲止沒有成功。 – EyesClear

+0

看到我的回答下面 – NSimon

回答

1

好的,我會盡量在這裏提供比我第一次評論更多的信息。 所以首先,系統不應該允許這種行爲,因爲在該線程討論:https://groups.google.com/forum/#!topic/android-platform/d6Kt1DhCAtw

話雖這麼說,我碰到這個問題時,願意在我的應用程序會自動UI測試來了,尤其是對於圖像處理,我想在屏幕上模擬手指。

這裏,你可能會應用到您的Activity B一個片段:

第1步:通過從Activity A最後MotionEventXY座標Activity B

步驟2:Activity B檢索這些值,並然後應用:

private void continueScrolling(int posX, int posY) { 
    long downTime = SystemClock.uptimeMillis(); 
    long eventTime = SystemClock.uptimeMillis(); 

    MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[1]; 
    MotionEvent.PointerCoords pc1 = new MotionEvent.PointerCoords(); 
    pc1.x = posX; 
    pc1.y = posY; 
    pc1.pressure = 1; 
    pc1.size = 1; 
    pointerCoords[0] = pc1; 

    MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[1]; 
    MotionEvent.PointerProperties pp1 = new MotionEvent.PointerProperties(); 
    pp1.id = 0; 
    pp1.toolType = MotionEvent.TOOL_TYPE_FINGER; 
    pointerProperties[0] = pp1; 

    MotionEvent event; 
    // send the initial touches (this seems to be to "wake up" the view, you might not need it in a non-testing context though) 
    event = MotionEvent.obtain(downTime, eventTime, 
      MotionEvent.ACTION_DOWN, 1, pointerProperties, pointerCoords, 
      0, 0, // metaState, buttonState 
      1, // x precision 
      1, // y precision 
      0, 0, // deviceId, edgeFlags 
      InputDevice.SOURCE_TOUCHSCREEN, 0); // source, flags 
    theViewYouWantTomove.dispatchGenericMotionEvent(event); 

    event = MotionEvent.obtain(downTime, eventTime, 
      MotionEvent.ACTION_DOWN, 
      1, pointerProperties, pointerCoords, 0, 0, 1, 1, 0, 0, 
      InputDevice.SOURCE_TOUCHSCREEN, 0); 
    theViewYouWantTomove.dispatchGenericMotionEvent(event); 
} 
+0

謝謝。我沒有看到您發佈的鏈接與此問題有關。無論如何,我試圖按照你的想法,但沒有運氣。 – EyesClear

+0

好吧,鏈接的OP正試圖實現與你一樣的模擬點擊視圖。 – NSimon

相關問題