Android的ViewPager与ScrollViews与ScrollViews内ViewPagers [英] Android ViewPager with ScrollViews with ViewPagers inside the ScrollViews

查看:154
本文介绍了Android的ViewPager与ScrollViews与ScrollViews内ViewPagers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有我的活动有一个主ViewPager和ViewPager内的每一页都有的全部内容作为一个滚动型并在该滚动型的还有另外一个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包含新闻文章,而文章很长所以是一个滚动型和滚动型里面有多个缩略图/照片,他们可以通过和刷卡。

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.

我已经尝试了一些不同的自定义ViewPagers不同的触摸事件的拦截,但似乎无法得到它的完美。它要么将完全吸收所有的触摸事件,使得滚动型的垂直滚动没有在这方面的工作,也将是非常敏感/很难得到内层一个水平滚动。

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.

任何人都有一个完美的解决方案?

Anyone have a perfect solution?

推荐答案

如果有人想知道我的解决办法:

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与ScrollViews与ScrollViews内ViewPagers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆