How to disable horizontal scrolling in Android webview

前端 未结 8 543
星月不相逢
星月不相逢 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:11

    Here is how I disable horizontal scrolling only for a webview.

    webView.setHorizontalScrollBarEnabled(false);
    webView.setOnTouchListener(new View.OnTouchListener() {
        float m_downX;
        public boolean onTouch(View v, MotionEvent event) {
    
            if (event.getPointerCount() > 1) {
                //Multi touch detected
                return true;
            }
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    // save the x
                    m_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(m_downX, event.getY());
                    break;
                }
    
            }
            return false;
        }
    });
    

    Basically intercept the touch event and don't allow the x value to change. This allows the webview to scroll vertically but not horizontally. Do it for y if you want the opposite.

提交回复
热议问题