Android 在 webview 中禁用滚动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2527899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:11:23  来源:igfitidea点击:

Disable scrolling in webview?

androidandroid-webviewandroid-scrollandroid-scrollbar

提问by Jake Sankey

Until now I have been an iPhone developer only and now I have decided to give Android a whirl. Something I haven't been able to figure out on Android is how to programmatically prevent scrolling in a WebView?

到目前为止,我只是一名 iPhone 开发人员,现在我决定试一试 Android。我在 Android 上无法弄清楚的是如何以编程方式防止在WebView?

Something similar to iPhones prevention of the onTouchMoveevent would be great!

类似于 iPhone 的onTouchMove事件预防会很棒!

回答by peceps

Here is my code for disabling all scrollingin webview:

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

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


To only hide the scrollbars, but not disable scrolling:

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

WebView.setVerticalScrollBarEnabled(false);
WebView.setHorizontalScrollBarEnabled(false);


or you can try using single column layoutbut this only works with simple pages and it disables horizontal scrolling:

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

   //Only disabled the horizontal scrolling:
   webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);


You can also try to wrap your webviewwith vertically scrolling scrollview and disable all scrolling on the webview:

您还可以尝试使用垂直滚动滚动视图包装您的 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>

And set

并设置

webview.setScrollContainer(false);

Don't forget to add the webview.setOnTouchListener(...)code above to disable all scrolling in the webview. The vertical ScrollView will allow for scrolling of the WebView's content.

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

回答by umar

I don't know if you still need it or not, but here is the solution:

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

appView = (WebView) findViewById(R.id.appView); 
appView.setVerticalScrollBarEnabled(false);
appView.setHorizontalScrollBarEnabled(false);

回答by GDanger

Making the WebViewignore motion events is the wrong way to go about it. What if the WebViewneeds to hear about these events?

使得WebView忽略运动的事件是错误的方式去了解它。如果WebView需要了解这些事件怎么办?

Instead subclass WebViewand override the non-private scroll methods.

相反,子类化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
    }
}

If you look at the sourcefor WebViewyou can see that onTouchEventcalls doDragwhich calls overScrollBy.

如果你看一下WebView您可以看到onTouchEvent来电doDrag这就要求overScrollBy

回答by Auri Rahimzadeh

Adding the margin details to the body will prevent scrolling if your content properly wraps, as so:

如果您的内容正确换行,则将边距详细信息添加到正文将防止滚动,如下所示:

<body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">

Easy enough, and a lot less code + bug overhead :)

足够简单,而且代码和错误开销要少得多:)

回答by user322987

Set a listener on your WebView:

在你的 WebView 上设置一个监听器:

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

回答by ZeroTruths

I haven't tried this as I have yet to encounter this problem, but perhaps you could overrive the onScroll function?

我还没有尝试过,因为我还没有遇到过这个问题,但也许你可以覆盖 onScroll 函数?

@Override
public void scrollTo(int x, int y){
super.scrollTo(0,y);
}

回答by user289463

My dirty, but easy-to-implement and well working solution:

我肮脏但易于实施且运行良好的解决方案:

Simply put the webview inside a scrollview. Make the webview to be far too bigger than the possible content (in one or both dimensions, depending on the requirements). ..and set up the scrollview's scrollbar(s) as you wish.

只需将 webview 放在滚动视图中。使 webview 远大于可能的内容(在一个或两个维度上,取决于要求)。..并根据需要设置滚动视图的滚动条。

Example to disable the horizontal scrollbar on a webview:

在 webview 上禁用水平滚动条的示例:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical"
>
    <WebView
        android:id="@+id/mywebview"
        android:layout_width="1000dip"
        android:layout_height="fill_parent"
    />
</ScrollView>

I hope this helps ;)

我希望这有帮助 ;)

回答by Saba Chaudry

To Disable scroll use this

要禁用滚动使用此

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

回答by Abhinav Saxena

I resolved the flicker and auto-scroll by:

我通过以下方式解决了闪烁和自动滚动:

webView.setFocusable(false);
webView.setFocusableInTouchMode(false);

However if it still does not work for some reason, simply make a class that extends WebView, and put this method on it:

但是,如果由于某种原因它仍然不起作用,只需创建一个扩展 WebView 的类,并将此方法放在其上:

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

I am suggesting to extend WebView, in order to provide more effective control, like disable/enable swipes, enable/disable touches, listeners, control transitions, animations, define settings internally so on..

我建议扩展 WebView,以提供更有效的控制,例如禁用/启用滑动、启用/禁用触摸、侦听器、控制转换、动画、内部定义设置等。

回答by Robert Etheredge

This may not be the source of your problem but I have spent hours trying to track down why my application that worked fine on the iphone cause the android browser to constantly throw up the vertical/horizontal scrollbars and actually move the page. I had an html body tag with height and width set to 100%, and the body was full of various tags in different locations. No matter what I tried, the android browsers would show their scroll bars and move the page just a little bit, particularly when doing a fast swipe. The solution that worked for me without having to do anything in an APK was to add the following to the body tag:

这可能不是您的问题的根源,但我花了几个小时试图找出为什么我的应用程序在 iphone 上运行良好导致 android 浏览器不断抛出垂直/水平滚动条并实际移动页面。我有一个高度和宽度设置为 100% 的 html 正文标签,正文在不同位置充满了各种标签。无论我尝试什么,Android 浏览器都会显示它们的滚动条并稍微移动页面,尤其是在快速滑动时。对我有用而无需在 APK 中执行任何操作的解决方案是将以下内容添加到 body 标记中:

leftmargin="0" topmargin="0"

It seems the default margins for body tag were being applied on the droid but ignored on the iphone

似乎身体标签的默认边距已应用于机器人,但在 iphone 上被忽略