Two dimensional scrolling… a suggestion that needs feedback

后端 未结 1 737
清酒与你
清酒与你 2020-12-12 01:59

While spending a copious amount of time googling for a relatively simple solution to my problem I found this as a solution for two-dimensional scrolling. I have a a horizon

1条回答
  •  一生所求
    2020-12-12 02:31

    Im not sure about the blog you have posted, this was my solution:

    /**
     * This class disables Y-motion on touch event.
     * It should only be used as parent class of HorizontalScrollView
     */
    public class ParentScrollView extends ScrollView {
        private GestureDetector mGestureDetector;
        View.OnTouchListener mGestureListener;
    
        @SuppressWarnings("deprecation")
        public ParentScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mGestureDetector = new GestureDetector(new YScrollDetector());
            setFadingEdgeLength(0);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if( mGestureDetector.onTouchEvent(ev)&super.onInterceptTouchEvent(ev)){
                return true;
            }else{
    
                return false;
            }
        }
    
        // 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;
            }
        }
    }
    

    XML:

        
    
            
    
    
            
        
    

    Basically the parent scrollview which only scrolls veritcal will be disabled because you will use a new custom class. Then you put a HScrollview within the scroll view. The Parentscroll view will pass the touch even if its not vertical to the horiszontalscroll view which makes it 2D scrolling effect.

    0 讨论(0)
提交回复
热议问题