Solution to the problem that viewpager cannot slide normally in Scrollview for Android programming development

This article describes the solution to the problem that viewpager cannot slide normally in Scrollview developed by Android programming. Share with you for your reference, as follows:

Here we mainly introduce how to solve the problem that viewpager often fails to slide in Scrollview and cannot slide normally. The solution is that when Scrollview is scrolling horizontally, the event is not handled by Scrollview, but is handled by its child view (i.e. viewpager here). Override the onintercepttouchevent function of Scrollview, 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 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,MotionEvent e2,float distanceX,float distanceY) { 
      /** 
       * 如果我们滚动更接近水平方向,返回false,让子视图来处理它
       */ 
      return (Math.abs(distanceY) > Math.abs(distanceX)); 
    } 
  } 
} 

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>