【问题标题】:Continue scrolling from one Activity to another继续从一个活动滚动到另一个活动
【发布时间】:2016-08-15 18:39:30
【问题描述】:

我有两个活动:ActivityAActivityBActivityA 正在监听运动事件。当滚动事件发生时,ActivityB 会立即启动。

ActivityB 内部有一个可滚动的内容,我希望它立即开始滚动此内容,无需强迫用户抬起手指。过渡必须是透明的,滚动手势不应在活动之间停止,而是要平稳地继续。

换句话说,问题在于ActivityB 不会对运动事件做出反应,除非用户抬起手指并再次触摸屏幕。

我该如何解决这个问题?

【问题讨论】:

  • 不能把最后的MotionEvent信息从A传给B,然后在B启动后模拟一个ACTION_DOWN吗?
  • @NicolasSimon 我认为这是正确的方向,我一直在探索它,但到目前为止没有成功。

标签: android android-activity scroll gesture motionevent


【解决方案1】:

好的,我会尝试在此处提供比我的第一条评论更多的信息。 所以首先,系统不应该允许这种行为,正如该线程中所讨论的那样:https://groups.google.com/forum/#!topic/android-platform/d6Kt1DhCAtw

话虽如此,当我愿意对我的应用程序进行自动化 UI 测试时遇到了这个问题,尤其是对于图像处理,我想在屏幕上模拟手指。

这里有一个 sn-p,您可以将其应用于您的 Activity B

第 1 步:将最后一个 MotionEventXY 坐标从 Activity A 传递到 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);
}

【讨论】:

  • 谢谢。我看不出您发布的链接与此问题有何关联。无论如何,我试图按照你的想法,但没有运气。
  • 好吧,链接的 OP 正在尝试实现与您相同的效果:模拟点击视图。
猜你喜欢
  • 2015-09-15
  • 2013-08-18
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
相关资源
最近更新 更多