ScrollView中的ViewPager不滚动correclty

我有一个“页面”上有很多组件,谁的内容比设备的高度还要长。 很好,只需把所有的布局(整个页面)放在一个ScrollView ,没问题。

其中一个组件是ViewPager 。 这个呈现正确,但对一个轻扫/一扔的反应是不正确的,这是紧张的,doens不总是工作。 它似乎与ScrollView “混淆”,所以只有当你在一个确切的水平线上ScrollView ,才能100%的工作。

如果我删除ScrollView ,ViewPager完美响应。

我有一个search,并没有发现这是一个已知的缺陷。 有没有人经历过这个?

平台版本:1.6兼容性库v4。
装置:macros达不可思议的S

任何帮助,将不胜感激。

下面是你testing的一些示例代码,注释掉ScrollView ,看它是否工作正常。

活动:

 package com.ss.activities; import com.ss.R; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.widget.TextView; public class PagerInsideScollViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewPager vp = (ViewPager) findViewById(R.id.viewpager); vp.setAdapter(new MyPagerAdapter(this)); } } class MyPagerAdapter extends PagerAdapter { private Context ctx; public MyPagerAdapter(Context context) { ctx = context; } @Override public int getCount() { return 2; } @Override public Object instantiateItem(View collection, int position) { TextView tv = new TextView(ctx); tv.setTextSize(50); tv.setTextColor(Color.WHITE); tv.setText("SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, " + "SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, " + "SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, SMILE DUDE, " + "SMILE DUDE, SMILE DUDE, SMILE DUDE"); ((ViewPager) collection).addView(tv); return tv; } @Override public void destroyItem(View collection, int position, Object view) { ((ViewPager) collection).removeView((View) view); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Parcelable saveState() { return null; } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { } @Override public void startUpdate(View arg0) { } @Override public void finishUpdate(View arg0) { } } 

布局:

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="300dp" /> </LinearLayout> </ScrollView> 

我有同样的问题。 我的解决scheme是在ViewPager滚动开始时调用requestDisallowInterceptTouchEvent

这是我的代码:

 pager.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { v.getParent().requestDisallowInterceptTouchEvent(true); return false; } }); pager.setOnPageChangeListener(new SimpleOnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { pager.getParent().requestDisallowInterceptTouchEvent(true); } }); 

进一步阅读显示滚动组件内部存在滚动组件的问题。

一种解决scheme是在所包含的可滚动组件的区域上“禁用”ScrollView的垂直滚动,在我的情况下是ViewPager。

这个解决scheme的代码在这里详细介绍(它简直太棒了!)

这是一个解决scheme:

  mPager.setOnTouchListener(new View.OnTouchListener() { int dragthreshold = 30; int downX; int downY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = (int) event.getRawX(); downY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int distanceX = Math.abs((int) event.getRawX() - downX); int distanceY = Math.abs((int) event.getRawY() - downY); if (distanceY > distanceX && distanceY > dragthreshold) { mPager.getParent().requestDisallowInterceptTouchEvent(false); mScrollView.getParent().requestDisallowInterceptTouchEvent(true); } else if (distanceX > distanceY && distanceX > dragthreshold) { mPager.getParent().requestDisallowInterceptTouchEvent(true); mScrollView.getParent().requestDisallowInterceptTouchEvent(false); } break; case MotionEvent.ACTION_UP: mScrollView.getParent().requestDisallowInterceptTouchEvent(false); mPager.getParent().requestDisallowInterceptTouchEvent(false); break; } return false; } }); 

当你按下时,基本上设置X,Y值,拖动时计算距离,以确定我们想要去哪个方向。 玩dragthreshold为您的情况进行优化。

使用ViewPager,您可以捕获页面更改事件,并防止ScrollView拦截导致页面更改的触摸事件。

这很简单,使用ViewGroup.requestDisallowInterceptTouchEvent(boolean) 。 即使他们在ViewPager上开始拖拽,它也可以让用户拖拽ScrollView,但是在页面上的水平拖拽仍然可以工作,而不会影响ScrollView。

  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Add android:id for your ScrollView in your layout final ScrollView sv = (ScrollView) findViewById(R.id.scrollview); final ViewPager vp = (ViewPager) findViewById(R.id.viewpager); vp.setAdapter(new MyPagerAdapter(this)); // Use a page-change listener to respond to begin-drag events: vp.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int newState) { if (newState == ViewPager.SCROLL_STATE_DRAGGING) { // Prevent the ScrollView from intercepting this event now that the page is changing. // When this drag ends, the ScrollView will start accepting touch events again. sv.requestDisallowInterceptTouchEvent(true); } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); } 

这适用于Android 2.3.4和4.2.1上的Android Support v4库。

我调整了@Michael Herbig的解决scheme,其优点是它可以在任何允许setOnTouchListener视图上setOnTouchListener ,而不仅仅是ViewPager(例如ViewPagerIndicator),它是一个自包含的类。

用法示例:

 // runStatsPager is a android.support.v4.view.ViewPager; runStatsPager.setOnTouchListener(new ViewInScrollViewTouchHelper(runStatsPager)); // runStatsPagerIndicator is a com.viewpagerindicator.TitlePageIndicator runStatsPagerIndicator.setOnTouchListener(new ViewInScrollViewTouchHelper(runStatsPagerIndicator)); 

和class级:

 class ViewInScrollViewTouchHelper implements View.OnTouchListener { private final ScrollView scrollView; private final View viewInScrollView; int dragthreshold = 30; int downX; int downY; public ViewInScrollViewTouchHelper(View viewInScrollView) { if (viewInScrollView == null) { throw new IllegalArgumentException("viewInScrollView cannot be null."); } ViewParent parent = viewInScrollView.getParent(); ScrollView scrollView = null; do { if (parent instanceof ScrollView) { scrollView = (ScrollView) parent; break; } } while(parent != null && (parent = parent.getParent()) != null); if (scrollView == null) { throw new IllegalArgumentException("View does not have a ScrollView in its parent hierarchy."); } this.scrollView = scrollView; this.viewInScrollView = viewInScrollView; } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = (int) event.getRawX(); downY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int distanceX = Math.abs((int) event.getRawX() - downX); int distanceY = Math.abs((int) event.getRawY() - downY); if (distanceY > distanceX && distanceY > dragthreshold) { viewInScrollView.getParent().requestDisallowInterceptTouchEvent(false); scrollView.getParent().requestDisallowInterceptTouchEvent(true); } else if (distanceX > distanceY && distanceX > dragthreshold) { viewInScrollView.getParent().requestDisallowInterceptTouchEvent(true); scrollView.getParent().requestDisallowInterceptTouchEvent(false); } break; case MotionEvent.ACTION_UP: scrollView.getParent().requestDisallowInterceptTouchEvent(false); viewInScrollView.getParent().requestDisallowInterceptTouchEvent(false); break; } return false; } } 
 public class WrapContentHeightViewPager extends ViewPager { public WrapContentHeightViewPager(Context context) { super(context); } public WrapContentHeightViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = 0; for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); int h = child.getMeasuredHeight(); if (h > height) height = h; } heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 

}

@Override public boolean onTouch(View v,MotionEvent event){

  int dragthreshold = 30; int downX = 0; int downY = 0; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = (int) event.getRawX(); downY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int distanceX = Math.abs((int) event.getRawX() - downX); int distanceY = Math.abs((int) event.getRawY() - downY); if (distanceY > distanceX && distanceY > dragthreshold) { mViewPager.getParent().requestDisallowInterceptTouchEvent(false); mScrollView.getParent().requestDisallowInterceptTouchEvent(true); } else if (distanceX > distanceY && distanceX > dragthreshold) { mViewPager.getParent().requestDisallowInterceptTouchEvent(true); mScrollView.getParent().requestDisallowInterceptTouchEvent(false); } break; case MotionEvent.ACTION_UP: mScrollView.getParent().requestDisallowInterceptTouchEvent(false); mViewPager.getParent().requestDisallowInterceptTouchEvent(false); break; } return false; } 

使用上面的两个,它会很好地工作。 我也用在我的代码中。

因此,通过这种方法,我让ViewPagerX方向上滚动,而不必担心ScrollView (父)窃取事件并取消当前滚动。 更重要的是,这也会使ViewPager所在的区域在Y方向上滚动。

 public class CustomViewPager extends ViewPager { private GestureDetector mGestureDetector; View.OnTouchListener mGestureListener; public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(context, new Detector()); } @Override public boolean onTouchEvent(MotionEvent motionEvent) { mGestureDetector.onTouchEvent(motionEvent); return super.onTouchEvent(motionEvent); } class Detector extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { requestDisallowInterceptTouchEvent(true); return false; } } } 

这两种解决scheme都不适合我,因为它可能是ViewPager触摸逻辑或ScrollView逻辑的修改。 我不得不实施这两个,现在它像一个魅力。

 public class TouchGreedyViewPager extends ViewPager { private float xDistance, yDistance, lastX, lastY; public TouchGreedyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: xDistance = yDistance = 0f; lastX = ev.getX(); lastY = ev.getY(); break; case MotionEvent.ACTION_MOVE: final float curX = ev.getX(); final float curY = ev.getY(); xDistance += Math.abs(curX - lastX); yDistance += Math.abs(curY - lastY) / 3; // favor X events lastX = curX; lastY = curY; if (xDistance > yDistance) { return true; } } return super.onInterceptTouchEvent(ev); } } public class TouchHumbleScrollView extends ScrollView { private float xDistance, yDistance, lastX, lastY; public TouchHumbleScrollView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: xDistance = yDistance = 0f; lastX = ev.getX(); lastY = ev.getY(); break; case MotionEvent.ACTION_MOVE: final float curX = ev.getX(); final float curY = ev.getY(); xDistance += Math.abs(curX - lastX); yDistance += Math.abs(curY - lastY) / 3; // favor X events lastX = curX; lastY = curY; if (xDistance > yDistance) { return false; } } return super.onInterceptTouchEvent(ev); } } 

您可以通过向每个页面添加一个scrollView来覆盖PagerAdapter类中的instantiateItem方法。 这是一个代码:

 @Override public Object instantiateItem(View collection, int position) { ScrollView sc = new ScrollView(cxt); sc.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); sc.setFillViewport(true); TextView tv = new TextView(cxt); tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); tv.setText(pages[position]); tv.setPadding(5,5,5,5); sc.addView(tv); ((ViewPager) collection).addView(sc); return sc; }