所以,我知道这个问题已经很老了,但我提出了一个解决方案,我认为它比这里发布的问题有一些额外的优势。可能发生的一种情况是,我的 ViewPager 中可能有多个 HorizontalScrollView(我确实这样做了),然后我需要向 ViewPager 提供大量 Id,以便它始终可以检查子触摸冲突。
我从ViewPager分析了源码,发现他们使用了这个叫做“canScroll”的方法来判断一个子视图是否可以滚动。幸运的是,它既不是静态的也不是最终的,所以我可以覆盖它。在该方法中调用了“boolean ViewCompat.canScrollHorizontally(View, int)”(对于 SDK
public class CustomViewPager extends ViewPager {
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomViewPager(Context context) {
super(context);
}
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
return super.canScroll(v, checkV, dx, x, y) || (checkV && customCanScroll(v));
}
protected boolean customCanScroll(View v) {
if (v instanceof HorizontalScrollView) {
View hsvChild = ((HorizontalScrollView) v).getChildAt(0);
if (hsvChild.getWidth() > v.getWidth())
return true;
}
return false;
}
}
因此,如果您的 ViewPager 中有不同类别的 View 也应该接收水平拖动,只需更改 customCanScroll(View) 以便在不应该拦截触摸时返回。
如果还应该检查当前视图的“可滚动性”,则布尔值 checkV 为真,这就是为什么我们也应该检查它。
希望这对未来的此类问题有所帮助(或者如果有更好的解决方案,或者来自 Android 平台的官方解决方案,请告诉我)。