ScrollView and Gallery interfering

前端 未结 5 1163
我在风中等你
我在风中等你 2020-12-16 06:44

I have a Gallery composed of many ScrollViews each of which occupies the whole screen. problem is the ScrollViews\' onTouchEvent returns true and therefore prevent any other

5条回答
  •  無奈伤痛
    2020-12-16 07:19

    This has given me headaches and I thought I'd post a solution based on chrisschell's answer because it helped me a lot.

    Here's the new and improved gallery.

    public class FriendlyGallery extends Gallery {
        FriendlyScrollView currScrollView;
    
        public FriendlyGallery(Context context) {
            super(context);
        }
        public FriendlyGallery(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            return super.onTouchEvent(ev);  
        }
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            currScrollView = getCurrScrollView();
            return super.onInterceptTouchEvent(ev);     
        }
        @Override
        public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if(currScrollView != null)
                currScrollView.scrollBy(0, (int) distanceY);
            return super.onScroll(e1, e2, distanceX, distanceY);
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if(currScrollView != null)
                currScrollView.fling(-(int) distanceY);
            return super.onFling(e1, e2, distanceX, distanceY);     
        }
    
        private FriendlyScrollView getCurrScrollView() {
            //I have a load more button that shouldn't be cast to a scrollview
            int pos = getFirstVisiblePosition();
            if(pos != getAdapter().getCount()-1)
                return (FriendlyScrollView)this.getSelectedView();
            else
                return null;
        }
    }
    

    And the scrollview.

    public class FriendlyScrollView extends ScrollView {
    
        public FriendlyScrollView(Context context) {
            super(context);
        }
        public FriendlyScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            return false;
        }
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            return false;       
        }
    }
    

    Hope this helps and thanks again to chrisschell for pointing me in the right direction.

提交回复
热议问题