如何更新webview?

时间:2016-07-12 09:39:57

标签: android webview

我需要更新我的网页视图,因为直到我旋转屏幕才显示任何内容。

我的网页浏览

  @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.myfragment3, container, false);
    webview = (WebView)v.findViewById(R.id.WebView);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    webview.setWebChromeClient(new WebChromeClient());
    String url = "http://www.acr-sl.com/catalogo/1/1/1";
    webview.loadUrl(url);
    return v;
}

UPDATE:最后解决了这个问题,从0 ina新片段创建webview,它唯一改变的是布局,所以问题是从我想的那里得出的。

4 个答案:

答案 0 :(得分:0)

您可以将它用于WebView webview.reload()

答案 1 :(得分:0)

你可以webView.reload()但请注意,如果请求是POST,这会重新发布一个页面,因此只能在GET上正常工作。 如果您的网址是静态的或相同的,那么您应该再次使用loadURL,这样会更好。

YourActivity.this.webView.loadUrl("http://www.website.com");

答案 2 :(得分:0)

下面是您的要求的工作代码,请将其视为>

布局文件如下:

  <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lenovo.webview.MainActivity">

    <WebView
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="@+id/activity_main"
        app:layout_constraintLeft_toLeftOf="@+id/activity_main"
        app:layout_constraintRight_toRightOf="@+id/activity_main"
        app:layout_constraintTop_toTopOf="@+id/activity_main" />

</android.support.constraint.ConstraintLayout>

Mainfest文件如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lenovo.webview">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java如下:

package com.example.lenovo.webview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
    WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.WebView);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webview.getSettings().setSupportZoom(true);
        webview.loadUrl("http://www.acr-sl.com/catalogo/1/1/1");
    }
}

答案 3 :(得分:0)

我的布局文件fragment_meru_detail为

<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"
    >
<ProgressBar

    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pageload"
        android:layout_gravity="center"
        android:text="Loding........"
        />

<WebView

    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:id="@+id/meruweb"
    ></WebView>
</LinearLayout>

我的Java类如下:

package com.rayz.digitalpostbox.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.rayz.digitalpostbox.R;
import com.rayz.digitalpostbox.activities.HomeActivity;


public class MeruDetail extends Fragment {

    TextView pageload ;
    WebView webview ;
    private ProgressBar progres;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_meru_detail, container, false);
        ((HomeActivity)getActivity()).setTitle("MERU Cabs");
         webview = (WebView) rootView.findViewById(R.id.meruweb);
        pageload = (TextView) rootView.findViewById(R.id.pageload);
        progres = (ProgressBar)rootView.findViewById(R.id.progressBar);
        progres.setMax(100);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webview.getSettings().setSupportZoom(true);
        webview.loadUrl("http://merucabs.com/mobile/");




        webview.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView view, int progress) {
                MeruDetail.this.setValue(progress);
                super.onProgressChanged(view, progress);
                if(progress==100){
                    progres.setVisibility(View.GONE);
                }else {
                    progres.setVisibility(View.VISIBLE);
                }

                if(progress>40){
                    pageload.setVisibility(View.GONE);
                }
            }

        });

        webview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
            }
        });


        MeruDetail.this.progres.setProgress(0);

        return  rootView ;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void setValue(int progress) {
        this.progres.setProgress(progress);
    }

    @Override
    public void onPause() {
        super.onPause();
        ((HomeActivity)getActivity()).setTitle("Dashboard");
    }
}

我认为您还应该检查您的mainfest文件以获取互联网许可

<uses-permission android:name="android.permission.INTERNET" />