NestedScrollView inside VerticalPager doesn't work properly












0















I created this extend class from NestedScrollView to dispatch to parent (my ViewPager) the event to move the view pager when the user touch down from the scoll view. This behavior works well.



The scrollview is indicated with the red rectangle:



enter image description here



BUT: if the scroll view is at bottom and if I want to swipe (from the scroll view!) to the next screen (below) the viewpager doesn't do anything. How can I do that guys?



Thank you very much!



public class VerticalNestedScrollview extends NestedScrollView {

public class VerticalPagerTouchDownEvent extends BaseEvent {
}

private long mDownTime;
private final long mAreaThreshold = 60; // In pixels
private boolean mCatchMoveTouchEvent = true;
private int mDownX, mDownY, mPagesCount = 0;
private ViewPager mParentViewPager;
private boolean mIsBottomReached = false, mIsTopReached = true;

public VerticalNestedScrollview(Context context) {
super(context);
init();
}

public VerticalNestedScrollview(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public VerticalNestedScrollview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

private void init() {
ViewTreeObserver observer = getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mParentViewPager = getViewPagerParent();
if (mParentViewPager != null) {
PagerAdapter pagerAdapter = mParentViewPager.getAdapter();
mPagesCount = pagerAdapter.getCount();
}
int contentHeight = getChildAt(0).getHeight();
mCatchMoveTouchEvent = getHeight() < contentHeight + getPaddingTop() + getPaddingBottom();
}
});
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);

refreshTopBottomReachedInfos();

super.onScrollChanged(l, t, oldl, oldt);
}

private void refreshTopBottomReachedInfos() {
mIsTopReached = getScrollY() == 0;

// Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
View view = getChildAt(getChildCount() - 1);

// Calculate the scroll diff
int diff = (view.getBottom() - (getHeight() + getScrollY())) + getPaddingBottom();

// if diff is zero, then the bottom has been reached
mIsBottomReached = diff <= 0;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (mCatchMoveTouchEvent) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
mDownTime = SystemClock.uptimeMillis();
mDownX = (int) ev.getRawX();
mDownY = (int) ev.getRawY();

refreshTopBottomReachedInfos();

} else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
Rect rect = new Rect();
getHitRect(rect);
rect.bottom += mAreaThreshold;
rect.top -= mAreaThreshold;
boolean isDownSwipe = mDownY > (int) ev.getRawY();

// The move action is outside this scrollview
if (!rect.contains((int) ev.getX(), (int) ev.getY())) {
dispatchTouchDownToViewPager(ev, isDownSwipe, 0);
return true;
}

} else {
mIsBottomReached = false;
mIsTopReached = false;
}

return super.dispatchTouchEvent(ev);

} else {
return true;
}
}

private boolean checkDispatchValidity(boolean isDownSwipe) {
if (isDownSwipe && mParentViewPager != null &&
mParentViewPager.getCurrentItem() == mPagesCount - 1)
return false;

if (!isDownSwipe && mParentViewPager != null &&
mParentViewPager.getCurrentItem() == 0)
return false;

return true;
}

private void dispatchTouchDownToViewPager(MotionEvent ev, boolean isDownSwipe, long deltaY) {
if (checkDispatchValidity(isDownSwipe)) {
MotionEvent motionEvent = MotionEvent.obtain(
mDownTime,
SystemClock.uptimeMillis() + 1000,
MotionEvent.ACTION_DOWN, (int) ev.getRawX(),
(int) ev.getRawY() - deltaY, 0
);

if (mParentViewPager != null) {
mParentViewPager.dispatchTouchEvent(motionEvent);
EventBus.getDefault().post(new VerticalPagerTouchDownEvent());
}
}
}

private ViewPager getViewPagerParent() {
ViewParent parent = getParent();
do {
if (parent instanceof ViewPager) {
return ((ViewPager) parent);
}
parent = parent.getParent();
} while (parent != null);
return null;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return true;
}
}









share|improve this question



























    0















    I created this extend class from NestedScrollView to dispatch to parent (my ViewPager) the event to move the view pager when the user touch down from the scoll view. This behavior works well.



    The scrollview is indicated with the red rectangle:



    enter image description here



    BUT: if the scroll view is at bottom and if I want to swipe (from the scroll view!) to the next screen (below) the viewpager doesn't do anything. How can I do that guys?



    Thank you very much!



    public class VerticalNestedScrollview extends NestedScrollView {

    public class VerticalPagerTouchDownEvent extends BaseEvent {
    }

    private long mDownTime;
    private final long mAreaThreshold = 60; // In pixels
    private boolean mCatchMoveTouchEvent = true;
    private int mDownX, mDownY, mPagesCount = 0;
    private ViewPager mParentViewPager;
    private boolean mIsBottomReached = false, mIsTopReached = true;

    public VerticalNestedScrollview(Context context) {
    super(context);
    init();
    }

    public VerticalNestedScrollview(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    }

    public VerticalNestedScrollview(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
    }

    private void init() {
    ViewTreeObserver observer = getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
    mParentViewPager = getViewPagerParent();
    if (mParentViewPager != null) {
    PagerAdapter pagerAdapter = mParentViewPager.getAdapter();
    mPagesCount = pagerAdapter.getCount();
    }
    int contentHeight = getChildAt(0).getHeight();
    mCatchMoveTouchEvent = getHeight() < contentHeight + getPaddingTop() + getPaddingBottom();
    }
    });
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);

    refreshTopBottomReachedInfos();

    super.onScrollChanged(l, t, oldl, oldt);
    }

    private void refreshTopBottomReachedInfos() {
    mIsTopReached = getScrollY() == 0;

    // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
    View view = getChildAt(getChildCount() - 1);

    // Calculate the scroll diff
    int diff = (view.getBottom() - (getHeight() + getScrollY())) + getPaddingBottom();

    // if diff is zero, then the bottom has been reached
    mIsBottomReached = diff <= 0;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mCatchMoveTouchEvent) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
    mDownTime = SystemClock.uptimeMillis();
    mDownX = (int) ev.getRawX();
    mDownY = (int) ev.getRawY();

    refreshTopBottomReachedInfos();

    } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
    Rect rect = new Rect();
    getHitRect(rect);
    rect.bottom += mAreaThreshold;
    rect.top -= mAreaThreshold;
    boolean isDownSwipe = mDownY > (int) ev.getRawY();

    // The move action is outside this scrollview
    if (!rect.contains((int) ev.getX(), (int) ev.getY())) {
    dispatchTouchDownToViewPager(ev, isDownSwipe, 0);
    return true;
    }

    } else {
    mIsBottomReached = false;
    mIsTopReached = false;
    }

    return super.dispatchTouchEvent(ev);

    } else {
    return true;
    }
    }

    private boolean checkDispatchValidity(boolean isDownSwipe) {
    if (isDownSwipe && mParentViewPager != null &&
    mParentViewPager.getCurrentItem() == mPagesCount - 1)
    return false;

    if (!isDownSwipe && mParentViewPager != null &&
    mParentViewPager.getCurrentItem() == 0)
    return false;

    return true;
    }

    private void dispatchTouchDownToViewPager(MotionEvent ev, boolean isDownSwipe, long deltaY) {
    if (checkDispatchValidity(isDownSwipe)) {
    MotionEvent motionEvent = MotionEvent.obtain(
    mDownTime,
    SystemClock.uptimeMillis() + 1000,
    MotionEvent.ACTION_DOWN, (int) ev.getRawX(),
    (int) ev.getRawY() - deltaY, 0
    );

    if (mParentViewPager != null) {
    mParentViewPager.dispatchTouchEvent(motionEvent);
    EventBus.getDefault().post(new VerticalPagerTouchDownEvent());
    }
    }
    }

    private ViewPager getViewPagerParent() {
    ViewParent parent = getParent();
    do {
    if (parent instanceof ViewPager) {
    return ((ViewPager) parent);
    }
    parent = parent.getParent();
    } while (parent != null);
    return null;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
    super.onTouchEvent(ev);
    return true;
    }
    }









    share|improve this question

























      0












      0








      0








      I created this extend class from NestedScrollView to dispatch to parent (my ViewPager) the event to move the view pager when the user touch down from the scoll view. This behavior works well.



      The scrollview is indicated with the red rectangle:



      enter image description here



      BUT: if the scroll view is at bottom and if I want to swipe (from the scroll view!) to the next screen (below) the viewpager doesn't do anything. How can I do that guys?



      Thank you very much!



      public class VerticalNestedScrollview extends NestedScrollView {

      public class VerticalPagerTouchDownEvent extends BaseEvent {
      }

      private long mDownTime;
      private final long mAreaThreshold = 60; // In pixels
      private boolean mCatchMoveTouchEvent = true;
      private int mDownX, mDownY, mPagesCount = 0;
      private ViewPager mParentViewPager;
      private boolean mIsBottomReached = false, mIsTopReached = true;

      public VerticalNestedScrollview(Context context) {
      super(context);
      init();
      }

      public VerticalNestedScrollview(Context context, AttributeSet attrs) {
      super(context, attrs);
      init();
      }

      public VerticalNestedScrollview(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      init();
      }

      private void init() {
      ViewTreeObserver observer = getViewTreeObserver();
      observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
      mParentViewPager = getViewPagerParent();
      if (mParentViewPager != null) {
      PagerAdapter pagerAdapter = mParentViewPager.getAdapter();
      mPagesCount = pagerAdapter.getCount();
      }
      int contentHeight = getChildAt(0).getHeight();
      mCatchMoveTouchEvent = getHeight() < contentHeight + getPaddingTop() + getPaddingBottom();
      }
      });
      }

      @Override
      protected void onScrollChanged(int l, int t, int oldl, int oldt) {
      super.onScrollChanged(l, t, oldl, oldt);

      refreshTopBottomReachedInfos();

      super.onScrollChanged(l, t, oldl, oldt);
      }

      private void refreshTopBottomReachedInfos() {
      mIsTopReached = getScrollY() == 0;

      // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
      View view = getChildAt(getChildCount() - 1);

      // Calculate the scroll diff
      int diff = (view.getBottom() - (getHeight() + getScrollY())) + getPaddingBottom();

      // if diff is zero, then the bottom has been reached
      mIsBottomReached = diff <= 0;
      }

      @Override
      public boolean dispatchTouchEvent(MotionEvent ev) {
      if (mCatchMoveTouchEvent) {
      if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      mDownTime = SystemClock.uptimeMillis();
      mDownX = (int) ev.getRawX();
      mDownY = (int) ev.getRawY();

      refreshTopBottomReachedInfos();

      } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
      Rect rect = new Rect();
      getHitRect(rect);
      rect.bottom += mAreaThreshold;
      rect.top -= mAreaThreshold;
      boolean isDownSwipe = mDownY > (int) ev.getRawY();

      // The move action is outside this scrollview
      if (!rect.contains((int) ev.getX(), (int) ev.getY())) {
      dispatchTouchDownToViewPager(ev, isDownSwipe, 0);
      return true;
      }

      } else {
      mIsBottomReached = false;
      mIsTopReached = false;
      }

      return super.dispatchTouchEvent(ev);

      } else {
      return true;
      }
      }

      private boolean checkDispatchValidity(boolean isDownSwipe) {
      if (isDownSwipe && mParentViewPager != null &&
      mParentViewPager.getCurrentItem() == mPagesCount - 1)
      return false;

      if (!isDownSwipe && mParentViewPager != null &&
      mParentViewPager.getCurrentItem() == 0)
      return false;

      return true;
      }

      private void dispatchTouchDownToViewPager(MotionEvent ev, boolean isDownSwipe, long deltaY) {
      if (checkDispatchValidity(isDownSwipe)) {
      MotionEvent motionEvent = MotionEvent.obtain(
      mDownTime,
      SystemClock.uptimeMillis() + 1000,
      MotionEvent.ACTION_DOWN, (int) ev.getRawX(),
      (int) ev.getRawY() - deltaY, 0
      );

      if (mParentViewPager != null) {
      mParentViewPager.dispatchTouchEvent(motionEvent);
      EventBus.getDefault().post(new VerticalPagerTouchDownEvent());
      }
      }
      }

      private ViewPager getViewPagerParent() {
      ViewParent parent = getParent();
      do {
      if (parent instanceof ViewPager) {
      return ((ViewPager) parent);
      }
      parent = parent.getParent();
      } while (parent != null);
      return null;
      }

      @Override
      public boolean onTouchEvent(MotionEvent ev) {
      super.onTouchEvent(ev);
      return true;
      }
      }









      share|improve this question














      I created this extend class from NestedScrollView to dispatch to parent (my ViewPager) the event to move the view pager when the user touch down from the scoll view. This behavior works well.



      The scrollview is indicated with the red rectangle:



      enter image description here



      BUT: if the scroll view is at bottom and if I want to swipe (from the scroll view!) to the next screen (below) the viewpager doesn't do anything. How can I do that guys?



      Thank you very much!



      public class VerticalNestedScrollview extends NestedScrollView {

      public class VerticalPagerTouchDownEvent extends BaseEvent {
      }

      private long mDownTime;
      private final long mAreaThreshold = 60; // In pixels
      private boolean mCatchMoveTouchEvent = true;
      private int mDownX, mDownY, mPagesCount = 0;
      private ViewPager mParentViewPager;
      private boolean mIsBottomReached = false, mIsTopReached = true;

      public VerticalNestedScrollview(Context context) {
      super(context);
      init();
      }

      public VerticalNestedScrollview(Context context, AttributeSet attrs) {
      super(context, attrs);
      init();
      }

      public VerticalNestedScrollview(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      init();
      }

      private void init() {
      ViewTreeObserver observer = getViewTreeObserver();
      observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
      mParentViewPager = getViewPagerParent();
      if (mParentViewPager != null) {
      PagerAdapter pagerAdapter = mParentViewPager.getAdapter();
      mPagesCount = pagerAdapter.getCount();
      }
      int contentHeight = getChildAt(0).getHeight();
      mCatchMoveTouchEvent = getHeight() < contentHeight + getPaddingTop() + getPaddingBottom();
      }
      });
      }

      @Override
      protected void onScrollChanged(int l, int t, int oldl, int oldt) {
      super.onScrollChanged(l, t, oldl, oldt);

      refreshTopBottomReachedInfos();

      super.onScrollChanged(l, t, oldl, oldt);
      }

      private void refreshTopBottomReachedInfos() {
      mIsTopReached = getScrollY() == 0;

      // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
      View view = getChildAt(getChildCount() - 1);

      // Calculate the scroll diff
      int diff = (view.getBottom() - (getHeight() + getScrollY())) + getPaddingBottom();

      // if diff is zero, then the bottom has been reached
      mIsBottomReached = diff <= 0;
      }

      @Override
      public boolean dispatchTouchEvent(MotionEvent ev) {
      if (mCatchMoveTouchEvent) {
      if (ev.getAction() == MotionEvent.ACTION_DOWN) {
      mDownTime = SystemClock.uptimeMillis();
      mDownX = (int) ev.getRawX();
      mDownY = (int) ev.getRawY();

      refreshTopBottomReachedInfos();

      } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
      Rect rect = new Rect();
      getHitRect(rect);
      rect.bottom += mAreaThreshold;
      rect.top -= mAreaThreshold;
      boolean isDownSwipe = mDownY > (int) ev.getRawY();

      // The move action is outside this scrollview
      if (!rect.contains((int) ev.getX(), (int) ev.getY())) {
      dispatchTouchDownToViewPager(ev, isDownSwipe, 0);
      return true;
      }

      } else {
      mIsBottomReached = false;
      mIsTopReached = false;
      }

      return super.dispatchTouchEvent(ev);

      } else {
      return true;
      }
      }

      private boolean checkDispatchValidity(boolean isDownSwipe) {
      if (isDownSwipe && mParentViewPager != null &&
      mParentViewPager.getCurrentItem() == mPagesCount - 1)
      return false;

      if (!isDownSwipe && mParentViewPager != null &&
      mParentViewPager.getCurrentItem() == 0)
      return false;

      return true;
      }

      private void dispatchTouchDownToViewPager(MotionEvent ev, boolean isDownSwipe, long deltaY) {
      if (checkDispatchValidity(isDownSwipe)) {
      MotionEvent motionEvent = MotionEvent.obtain(
      mDownTime,
      SystemClock.uptimeMillis() + 1000,
      MotionEvent.ACTION_DOWN, (int) ev.getRawX(),
      (int) ev.getRawY() - deltaY, 0
      );

      if (mParentViewPager != null) {
      mParentViewPager.dispatchTouchEvent(motionEvent);
      EventBus.getDefault().post(new VerticalPagerTouchDownEvent());
      }
      }
      }

      private ViewPager getViewPagerParent() {
      ViewParent parent = getParent();
      do {
      if (parent instanceof ViewPager) {
      return ((ViewPager) parent);
      }
      parent = parent.getParent();
      } while (parent != null);
      return null;
      }

      @Override
      public boolean onTouchEvent(MotionEvent ev) {
      super.onTouchEvent(ev);
      return true;
      }
      }






      android android-viewpager android-nestedscrollview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 16:29









      anthonyanthony

      2,63862458




      2,63862458
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469538%2fnestedscrollview-inside-verticalpager-doesnt-work-properly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469538%2fnestedscrollview-inside-verticalpager-doesnt-work-properly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'