Gallery 内的 ScrollView,两者都独立滚动

编程入门 行业动态 更新时间:2024-10-12 03:17:02
本文介绍了Gallery 内的 ScrollView,两者都独立滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有适配器的 Gallery,它提供 ScrollViews 作为它的子视图.我需要确保按预期正确处理触摸事件:

I have a Gallery with an adapter which supplies it ScrollViews as its child views. I need to make sure that the touch events are handled correctly and as expected:

  • 当用户水平滚动时,图库也会水平滚动.
  • 当用户垂直滚动时,滚动视图也会垂直滚动.
  • 绝不能在同一个手势上进行两次滚动(用户必须抬起手指才能滚动另一个视图).
  • 一切都必须平滑滚动.
  • 在不覆盖任何方法的情况下,滚动视图是唯一滚动的东西 - 图库从不滚动.

    Without overriding any methods, the scroll view is the only thing that scrolls - the gallery never scrolls.

    所以我知道我需要在图库中使用 onInterceptTouchEvent(...) 来决定接管某个系列的 MotionEvents,但我不确定如何检查触摸是水平的还是垂直的.

    So I understand I need to use onInterceptTouchEvent(...) in the gallery to decide to take over a certain series of MotionEvents but I am unsure how to check if the touch is horizontal or vertical in nature.

    推荐答案

    好的,经过一些主要的摆弄和 logcat hacking,这里是解决方案:

    OK, after some major fiddling and logcat hacking, here's the solution:

    public class SwipeInterceptingGallery extends Gallery { private float mInitialX; private float mInitialY; private boolean mNeedToRebase; private boolean mIgnore; public SwipeInterceptingGallery(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public SwipeInterceptingGallery(Context context, AttributeSet attrs) { super(context, attrs); } public SwipeInterceptingGallery(Context context) { super(context); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if (mNeedToRebase) { mNeedToRebase = false; distanceX = 0; } return super.onScroll(e1, e2, distanceX, distanceY); } @Override public boolean onInterceptTouchEvent(MotionEvent e) { switch (e.getAction()) { case MotionEvent.ACTION_DOWN: { mIgnore = false; mNeedToRebase = true; mInitialX = e.getX(); mInitialY = e.getY(); return false; } case MotionEvent.ACTION_MOVE: { if (!mIgnore) { float deltaX = Math.abs(e.getX() - mInitialX); float deltaY = Math.abs(e.getY() - mInitialY); mIgnore = deltaX < deltaY; return !mIgnore; } return false; } default: { return super.onInterceptTouchEvent(e); } } } }

    更多推荐

    Gallery 内的 ScrollView,两者都独立滚动

    本文发布于:2023-11-16 07:01:32,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1602045.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:独立   Gallery   ScrollView

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!