Solution to the problem of sliding Viewpager brought by Android _android

Source: Internet
Author: User

Problem Description:
when the Viewpager control is placed in the swiperefreshlayout, the sliding of the two will conflict with each other. The specific performance of the Viewpager left and right sliding is not smooth, easy to be swiperefreshlayout interception (that is, a refreshing view).

Problem Reason:
Viewpager itself is a conflict that handles scrolling events, and it calls Requestdisallowintercepttouchevent () when it slides sideways. Method so that the parent control does not intercept the current touch event sequence. But Swiperefreshlayout's requestdisallowintercepttouchevent () method does nothing, so it still blocks the current touch event sequence.

Problem Analysis:
Why does the Requestdisallowintercepttouchevent () method of swiperefreshlayout do nothing?

First swiperefreshlayout inherits from ViewGroup.

In the case where the Requestdisallowintercepttouchevent () method does nothing, the user can pull the loadingview from the bottom of the drop-down.
If the method calls the ViewGroup Requestdisallowintercepttouchevent () method, the Viewpager compatibility issue can be resolved, but the user cannot continue to pull down after pulling the bottom of the interface down to the head. You need to open your fingers once to pull out the loadingview.
Target Analysis:
So in order to scroll more smoothly, the desired effect is of course to pull out the Loadingview. The Requestdisallowintercepttouchevent () method is invoked when the Viewpager is sliding left and right. Then the swiperefreshlayout should only intercept the touch event while sliding up and down.

The specific logic is as follows:

Record whether the Requestdisallowintercepttouchevent () method was invoked and set to true.
In the swiperefreshlayout to determine whether it is sliding up and down.
Call Super.requestdisallowintercepttouchevent (TRUE) if 1,2 is satisfied at the same time.
Otherwise, call Super.requestdisallowintercepttouchevent (false).
Note: Since the ViewGroup Requestdisallowintercepttouchevent method sets True, the touch event is intercepted in the Dispatchtouchevent () method. Therefore, it is necessary to determine whether or not to slide up and down in the Dispatchtouchevent () method.

Implementation code (partial):

Illegal keys private static final int invalid_pointer =-1;

Dispatch method records the first pressed x private float minitialdispatchdownx;

Dispatch Method records the first press of Y private float minitialdispatchdowny;

Dispatch method records the finger private int mactivedispatchpointerid = Invalid_pointer;

Whether to request blocking private Boolean hasrequestdisallowintercept = false;
  @Override public void Requestdisallowintercepttouchevent (Boolean b) {hasrequestdisallowintercept = b;
Nope. @Override public boolean dispatchtouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent.action_d
      Own:mactivedispatchpointerid = Motioneventcompat.getpointerid (EV, 0);
      Final float Initialdownx = getmotioneventx (EV, Mactivedispatchpointerid);
      if (Initialdownx!= invalid_pointer) {minitialdispatchdownx = Initialdownx;
      Final float Initialdowny = getmotioneventy (EV, Mactivedispatchpointerid);
      if (Minitialdispatchdowny!= invalid_pointer) {minitialdispatchdowny = Initialdowny;
    }  Break  Case MotionEvent.ACTION_MOVE:if (hasrequestdisallowintercept) {//Resolve the Viewpager sliding conflict issue final float x =
        Getmotioneventx (EV, Mactivedispatchpointerid);
        Final float y = getmotioneventy (ev, Mactivedispatchpointerid); if (minitialdispatchdownx!= invalid_pointer && x!= invalid_pointer && minitialdispatchdowny
          != invalid_pointer && y!= invalid_pointer) {final float Xdiff = Math.Abs (X-MINITIALDISPATCHDOWNX);
          Final float Ydiff = Math.Abs (Y-minitialdispatchdowny); if (Xdiff > Mtouchslop && xdiff * 0.7f > Ydiff) {//horizontal scrolling does not need to intercept super.requestdisallowi
          Ntercepttouchevent (TRUE);
          else {super.requestdisallowintercepttouchevent (false);
        } else {super.requestdisallowintercepttouchevent (false);
    }} break;
   Case MotionEvent.ACTION_UP:case Motionevent.action_cancel:   if (ev.getaction () = = Motionevent.action_up | | ev.getaction () = = Motionevent.action_cancel) {Hasrequestdisallo
      Wintercept = false;
  } break;
return super.dispatchtouchevent (EV); Private float Getmotioneventy (motionevent ev, int activepointerid) {Final int index = Motioneventcompat.findpointeri
  Ndex (EV, Activepointerid);
  if (Index < 0) {return-1;
Return motioneventcompat.gety (EV, index); Private float Getmotioneventx (motionevent ev, int activepointerid) {Final int index = Motioneventcompat.findpointeri
  Ndex (EV, Activepointerid);
  if (Index < 0) {return-1;
Return Motioneventcompat.getx (EV, index);
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.