在webview中禁用滚动?

Jak*_*key 81 android android-webview android-scrollbar android-scroll

到目前为止,我只是一名iPhone开发人员,现在我决定给Android一个旋转.我在Android上无法弄清楚的是如何以编程方式阻止滚动WebView

类似于iPhone预防onTouchMove活动的东西会很棒!

pec*_*eps 170

这是我在webview中禁用所有滚动的代码:

  // disable scroll on touch
  webview.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      return (event.getAction() == MotionEvent.ACTION_MOVE);
    }
  });
Run Code Online (Sandbox Code Playgroud)

仅隐藏滚动条,但不禁用滚动:

WebView.setVerticalScrollBarEnabled(false);
WebView.setHorizontalScrollBarEnabled(false);
Run Code Online (Sandbox Code Playgroud)

或者您可以尝试使用单列布局,但这仅适用于简单页面并禁用水平滚动:

   //Only disabled the horizontal scrolling:
   webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
Run Code Online (Sandbox Code Playgroud)

您还可以尝试使用垂直滚动滚动视图包装 Webview并禁用webview上的所有滚动:

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"    >
  <WebView
    android:id="@+id/mywebview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none" />
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

并设置

webview.setScrollContainer(false);
Run Code Online (Sandbox Code Playgroud)

不要忘记添加webview.setOnTouchListener(...)上面的代码以禁用webview中的所有滚动.垂直ScrollView将允许滚动WebView的内容.

  • 如果将WebView放在ScrollView中,请将WebView的android:isScrollContainer属性设置为"false",而不是将android:scrollbars设置为"none".这具有完全禁用webview的滚动处理的效果,并允许其根据其内容进行扩展.然后,滚动将仅由父ScrollView处理. (3认同)

uma*_*mar 22

我不知道你是否仍然需要它,但这是解决方案:

appView = (WebView) findViewById(R.id.appView); 
appView.setVerticalScrollBarEnabled(false);
appView.setHorizontalScrollBarEnabled(false);
Run Code Online (Sandbox Code Playgroud)

  • 好.这会禁用滚动条,但它不会阻止通过拖动滚动.似乎html也必须适合...... (17认同)

GDa*_*ger 20

制作WebView忽略运动事件是错误的方法.如果WebView需要了解这些事件怎么办?

而是子类化WebView并覆盖非私有滚动方法.

public class NoScrollWebView extends WebView {
    ...
    @Override
    public boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, 
                                int scrollRangeX, int scrollRangeY, int maxOverScrollX, 
                                int maxOverScrollY, boolean isTouchEvent) {
        return false;
    }

    @Override
    public void scrollTo(int x, int y) {
        // Do nothing
    }

    @Override
    public void computeScroll() {
        // Do nothing
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你看看来源WebView你可以看到,onTouchEvent调用doDrag它调用overScrollBy.


Aur*_*deh 13

如果您的内容正确包装,则向主体添加边距详细信息将阻止滚动,如下所示:

<body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
Run Code Online (Sandbox Code Playgroud)

足够简单,代码少+ bug开销:)


小智 7

在WebView上设置一个监听器:

webView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return(event.getAction() == MotionEvent.ACTION_MOVE));
            }
        });
Run Code Online (Sandbox Code Playgroud)

  • 那里的`return`行末尾有一个额外的括号.这也打破了HTML5画布和JavaScript触摸事件. (3认同)

小智 6

我还没有尝试过,因为我还没有遇到这个问题,但也许你可能会过度使用onScroll功能?

@Override
public void scrollTo(int x, int y){
super.scrollTo(0,y);
}
Run Code Online (Sandbox Code Playgroud)