【问题标题】:ScrollView inside Gallery, both scrolling independentlyGallery里面的ScrollView,两者都独立滚动
【发布时间】:2011-03-12 22:55:26
【问题描述】:

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

  1. 当用户水平滚动时,图库会水平滚动。
  2. 当用户垂直滚动时,滚动视图会垂直滚动。
  3. 绝不应在同一个手势上同时进行两次滚动(用户必须抬起手指才能滚动另一个视图)。
  4. 一切都必须流畅滚动。

在不覆盖任何方法的情况下,滚动视图是唯一滚动的东西 - 图库永远不会滚动。

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

【问题讨论】:

    标签: android user-interface touch scrollview android-gallery


    【解决方案1】:

    好的,经过一些重大的摆弄和 logcat 黑客攻击,解决方案如下:

    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);
                }
            }
        }
    }
    

    【讨论】:

    • 什么情况下使用这个图库,能不能详细点,或者举个例子,非常感谢
    • @pengwang:这是当您将垂直可滚动视图作为您的画廊项目并且您希望能够滚动画廊(水平)和这些可滚动视图(垂直)时。
    • 有没有办法以编程方式或使用 d-pad 滚动这个画廊?
    • @Warlax - 漂亮!谢谢,一直在寻找解决我的问题的方法,才在这里找到:)
    【解决方案2】:

    我已经尝试过Warlax 提供的解决方案。它让我前进,但不幸的是,它在极少数情况下会破坏正常的画廊行为。 (例如它在滚动时不会停止触摸)所以我做了更多的研究并提出了以下解决方案。

    public class TouchInterceptingGallery extends Gallery {
    
        public TouchInterceptingGallery(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public TouchInterceptingGallery(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public TouchInterceptingGallery(Context context) {
            super(context);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            this.onTouchEvent(ev);
            return false;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      相关资源
      最近更新 更多