问题描述
所以我的 Activity 有一个主 ViewPager,在 ViewPager 内部每个页面都有一个 ScrollView 的全部内容,并且在该 ScrollView 内部还有另一个 ViewPager.
So I have my activity which has a main ViewPager and inside of the ViewPager each page has the whole content as a ScrollView and inside of that ScrollView there is another ViewPager.
这听起来可能很疯狂,但基本上外部 ViewPager 包含新闻文章,并且文章很长,因此有一个 ScrollView,并且在 ScrollView 内部有多个缩略图/图片,它们也可以滑动浏览.
This might sound crazy but basically the outer ViewPager contains news articles, and the articles are long so there is a ScrollView, and inside the ScrollView there are multiple thumbnails/pictures that they can swipe through as well.
我尝试了几种不同的自定义 ViewPager,它们具有不同的触摸事件拦截功能,但似乎无法做到完美.它要么完全吸收所有触摸事件,因此 ScrollView 的垂直滚动在该区域不起作用,要么让内部的水平滚动非常敏感/困难.
I've tried a few different custom ViewPagers with different touch event interception but can't seem to get it perfect. It either completely will absorb all touch events so that the vertical scrolling of the ScrollView doesn't work in that area, or it will be really touchy/difficult to get the inner one to scroll horizontally.
谁有完美的解决方案?
推荐答案
如果有人想知道我的解决方案:
In case anyone wants to know my solution:
public class CustomScrollView extends ScrollView {
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, new YScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev)
&& mGestureDetector.onTouchEvent(ev);
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
if (Math.abs(distanceY) > Math.abs(distanceX)) {
return true;
}
return false;
}
}
}
最外面的ViewPager是:
and the outer most ViewPager is:
public class NestingViewPager extends ViewPager {
public NestingViewPager(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public NestingViewPager(final Context context) {
super(context);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v != this && v instanceof ViewPager) {
return true;
}
return super.canScroll(v, checkV, dx, x, y);
}
}
这篇关于Android ViewPager with ScrollViews with ViewPagers inside the ScrollViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!