Android: HorizontalScrollView inside ScrollView

前端 未结 6 1890
忘掉有多难
忘掉有多难 2021-02-05 12:46

I have multiple HorizontalScrollViews inside a ScrollView. Horizontal scroll isn\'t smooth at all. I have to scroll almost perfectly horizontal

6条回答
  •  忘了有多久
    2021-02-05 12:57

    While David's answer works, it has a downside. It passes ScrollView's MotionEvent object directly to HorizontalScrollView.onTouchEvent(), so if HorizontalScrollView or its children try to get the event coordinates, they will get the wrong coordinates which based on ScrollView.

    My solution:

    public class CustomScrollView extends ScrollView{
    
        /*************skip initialization*************/
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent e){
            //returning false means ScrollView is not interested at any events,
            //so ScrollView's onTouchEvent() won't be called,
            //and all of the events will be passed to ScrollView's child
            return false;
        }
    
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            //manually call ScrollView's onTouchEvent(),
            //the vertical scrolling happens there.
            onTouchEvent(ev);
            //dispatch the event,
            //ScrollView's child will have every event.
            return super.dispatchTouchEvent(ev);
        }
    }
    

    Just wrap this CustomScrollView around the HorizontalScrollView in your layout file.

提交回复
热议问题