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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android ViewPager with ScrollViews with ViewPagers inside the ScrollViews
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
