How to disable horizontal scrolling in Android webview

前端 未结 8 512
星月不相逢
星月不相逢 2020-12-24 09:53

I want to have only vertical scrolling in my webview and don\'t want any horizontal scrolling.

webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

8条回答
  •  隐瞒了意图╮
    2020-12-24 10:01

    Solution based on @vee answer, but more convenient:

    public class WebViewTouchListener implements View.OnTouchListener {
        private float downX;
    
        @Override
        public boolean onTouch(final View v, final MotionEvent event) {
            if (event.getPointerCount() > 1) {
                //multi touch
                return true;
            }
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = event.getX();
                    break;
                case MotionEvent.ACTION_MOVE:
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    // set x so that it doesn't move
                    event.setLocation(downX, event.getY());
                    break;
            }
            return false;
        }
    }
    

    And just use it with a WebView:

     webView.setHorizontalScrollBarEnabled(false);
     webView.setOnTouchListener(new WebViewTouchListener());
    

提交回复
热议问题