Solution of Viewpager ScrollView in Android programming development _android

Source: Internet
Author: User
Tags abs

This article illustrates the solution to the problem of Viewpager in the Android programming development ScrollView. Share to everyone for your reference, specific as follows:

This article mainly describes how to solve the Viewpager in the ScrollView sliding often failure, not normal sliding problem.
The solution is to override the Onintercepttouchevent function of ScrollView by simply scrollview the event to its child view (that is, viewpager) while approaching horizontal scrolling, as follows:

Package Cc.newnews.view; 
Import Android.content.Context; 
Import Android.util.AttributeSet; 
Import Android.view.GestureDetector;
Import Android.view.GestureDetector.SimpleOnGestureListener; 
Import android.view.MotionEvent; 
Import Android.widget.ScrollView; 
  public class Verticalscrollview extends ScrollView {private Gesturedetector mgesturedetector; 
    Public Verticalscrollview (context, AttributeSet attrs) {Super (context, attrs);
  Mgesturedetector = new Gesturedetector (context, new Yscrolldetector ()); 
        @Override public boolean onintercepttouchevent (Motionevent ev) {return super.onintercepttouchevent (EV) 
  && mgesturedetector.ontouchevent (EV); Class Yscrolldetector extends Simpleongesturelistener {@Override public boolean onscroll (Motionevent E1, Mo 
      Tionevent E2, float Distancex, float distancey) {/** * If we scroll closer to the horizontal direction, return false and let the child view handle it. Return (Math.Abs (Distancey) > Math.Abs (Distancex)); 

 } 
  } 
}

Then change the ScrollView in the XML to <xxx.xxx.xxx.VerticalScrollView> that is the package name. Overridden ScrollView class name).

This method also applies to other view ListView such as ScrollView, which cannot be scrolled.

I hope this article will help you with the Android program.

Related Article

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.