Android WebView无法滚动 [英] Android WebView won't scroll

查看:210
本文介绍了Android WebView无法滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android(4.2)中有一个 WebView ,该加载了带有JavaScript(放置在资产文件夹中)的本地html .

I have a WebView in Android (4.2) which loads a local html with javascript (placed in the assets folder).

我的问题是,我无法在我的WebView中滚动.我通过类似的SO问题尝试了很多事情和建议,但没有任何效果.

My Problem is, I can't scroll in my WebView whatsoever. I Tried a lot of things and suggestions by similiar SO questions but nothing worked.

我的HTML只是一个简单的div-Tag,Kinetic-js框架使用它来在其上绘制图片和画布.

My Html is just a simple div-Tag which is used by the Kinetic-js framework to draw a picture and some canvas on top of it.

XML布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/text_size_smed_16sp"
            android:textColor="@android:color/white"
            android:text="@string/ui_floor_plan_building_textView" />

        <Spinner android:id="@+id/floor_plan_building_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/text_size_smed_16sp"
            android:textColor="@android:color/white"
            android:text="@string/ui_floor_plan_level_textView" />

        <Spinner android:id="@+id/floor_plan_level_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <AutoCompleteTextView android:id="@+id/floor_plan_room_autoTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:completionThreshold="1"
            android:hint="@string/ui_floor_plan_autocomplete_hint"
            android:inputType="number"
            android:imeOptions="actionSearch" />

    </LinearLayout>

     <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <WebView android:id="@+id/floor_plan_webView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

     </LinearLayout>

</LinearLayout>

Java onCreate:

    this.webView = (WebView) findViewById(R.id.floor_plan_webView);
    this.webView.getSettings().setJavaScriptEnabled(true);

    this.webView.getSettings().setLoadWithOverviewMode(true);
    this.webView.getSettings().setUseWideViewPort(true);

    this.webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    this.webView.setScrollbarFadingEnabled(false);
    this.webView.setVerticalScrollBarEnabled(true);
    this.webView.setHorizontalScrollBarEnabled(true);

    this.webView.getSettings().setSupportZoom(true);
    this.webView.getSettings().setLightTouchEnabled(true);

    this.webView.setWebViewClient(new WebViewClient(){

        private boolean initialLoad = true;

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if(this.initialLoad){
                this.initialLoad = false;
                drawFloorPlan();
            }
        }

    });
    this.webView.loadUrl(PATH_FLOOR_PLAN_HTML);

HTML文件:

    <html>
        <head>
        <title>Floor Plans</title>

        <meta charset="UTF-8">

        <script type="text/javascript" src="floor_plan.js"></script>
        <script type="text/javascript" src="kinetic-v4.0.5.js"></script>
    </head>

    <body onload="initialize()" style="margin:0px; padding:0px; width:100%; height:100%">
        <div id="container"></div>
    </body>

</html>

观察: 当显示的图像(html文件)小于Web视图时,只要触摸事件在显示的html的外部",就可以垂直和水平滚动.通常我的图片很大,因此根本没有机会触摸外部并滚动.

Observation: When the displayed image (html file) is smaller than the webview it is possible to scroll vertical and horizontal as long as the touch event is "outside" the shown html. Normally my picture is very large so there is no chance at all to touch outside and scroll.

我不确定WebView本身或html页面中是否存在问题.有什么想法可以让我恢复工作吗?

I am not sure if its a problem within WebView itself or the html page. Any ideas how i can get the scrolling back to work?

我非常感谢每个答案=)预先感谢!

I greatly appreciate every answer =) Thanks in advance!

推荐答案

我找到了解决方案:

我实现了自己的onTouchListener并自行滚动.从另一个SO问题/想法中得到了想法( Android:滚动Imageview )

I implemented my own onTouchListener and do the scrolling on my own. Got the idea from another SO quesition/answer (Android: Scrolling an Imageview)

认为这可能对其他人有所帮助,因为在WebView中似乎有些bug,其中包含从file:///...加载的某些html文件.

Thought this might be helpful to others as it seems to be a little buggy in the WebView with certain html files, loaded from file:///...

这篇关于Android WebView无法滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆