ScrollView inside Gallery, both scrolling independently

怎甘沉沦 提交于 2019-11-27 01:42:29

问题


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:

  1. When the user scrolls horizontally, the gallery scrolls horizontally.
  2. When the user scrolls vertically, the scroll view scrolls vertically.
  3. Both scrolls should never happen on the same gesture (the user must lift their finger to scroll the other view).
  4. Everything must scroll smoothly.

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

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.


回答1:


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



回答2:


I have tried solution provided by Warlax. It moved me forward but unfortunately it breaks normal gallery behavior in some rare cases. (For example it doesn't stop on touch while scrolling) So I did more research and came up with the following solution.

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

}


来源:https://stackoverflow.com/questions/5286115/scrollview-inside-gallery-both-scrolling-independently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!