如何将动作从一个viewpager传递到另一个viewpager?

时间:2015-10-08 14:11:04

标签: android android-viewpager

我想要2个视图寻呼机同时滚动。如果我滚动一个,第二个模仿它。 OnPageScrolledListener拥有所有必需的数据,但我不知道在哪里插入它。

new OnPageChangeListener() {

      @Override public void onPageScrolled(int page, float scale, int offset) {
      }

      @Override public void onPageSelected(int i) {
        setIndicators(i);
      }

      @Override public void onPageScrollStateChanged(int i) {

      }
    }

我试图复制动作,但它总是触发IlligalStateException

pager.setOnTouchListener(new View.OnTouchListener() {
      @Override public boolean onTouch(View v, MotionEvent event) {
        pagerInner.dispatchTouchEvent(event);
        return false;
      }
    });
java.lang.IllegalArgumentException: pointerIndex out of range
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.MotionEvent.nativeGetAxisValue(Native Method)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.MotionEvent.getX(MotionEvent.java:1981)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:110)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:462)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2054)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.View.dispatchTouchEvent(View.java:7127)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at yarh.bigdig.newreveil.view.activities.TutorialActivity$2.onTouch(TutorialActivity.java:90)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.View.dispatchTouchEvent(View.java:7122)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.View.dispatchPointerEvent(View.java:7307)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4203)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4243)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:523)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:615)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-08 14:23:32.580 25711-25711/yarh.bigdig.newreveil E/AndroidRuntime:  at dalvik.system.NativeStart.m

1 个答案:

答案 0 :(得分:0)

有点尖锐,但pagerInner模仿pagerOuter gra ;;

pagerOuter.addOnPageChangeListener(new OnPageChangeListener(){

  @Override public void onPageScrolled(int page, float scale, int offset) {
    if (currentPage == page) {
      if (part != 1) currentOffset = 0;
      part = 1;
    } else {
      if (part != -1) currentOffset = 0;
      part = -1;
    }
    if (currentOffset != 0 && pagerInner.isFakeDragging()
        && Math.abs(currentOffset) < width - 20) {
      pagerInner.fakeDragBy(-1 * ((offset - currentOffset)));
    }
    currentOffset = offset;
  }

  @Override public void onPageSelected(int i) {
    setIndicators(i);
    currentOffset = 0;
    currentPage = i;
    if (pagerInner.getCurrentItem() != i) pagerInner.setCurrentItem(i, true);
  }

  @Override public void onPageScrollStateChanged(int i) {
    switch (i) {
      case ViewPager.SCROLL_STATE_DRAGGING:
        pagerInner.beginFakeDrag();
        break;
      case ViewPager.SCROLL_STATE_IDLE:
        break;
      case ViewPager.SCROLL_STATE_SETTLING:
        pagerInner.endFakeDrag();
        break;
    }
  }
});