Android ViewPager with ScrollViews with ViewPagers inside the ScrollViews

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

问题描述

所以我的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)