这里是xml布局文件
[java] view plain copy
print?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
现在创建color.xml文件,用来展示进度条颜色
[java] view plain copy
print?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- A light Holo shade of blue -->
<color name="holo_blue_light">#ff33b5e5</color>
<!-- A light Holo shade of green -->
<color name="holo_green_light">#ff99cc00</color>
<!-- A light Holo shade of red -->
<color name="holo_red_light">#ffff4444</color>
<!-- A dark Holo shade of blue -->
<color name="holo_blue_dark">#ff0099cc</color>
<!-- A dark Holo shade of green -->
<color name="holo_green_dark">#ff669900</color>
<!-- A dark Holo shade of red -->
<color name="holo_red_dark">#ffcc0000</color>
<!-- A Holo shade of purple -->
<color name="holo_purple">#ffaa66cc</color>
<!-- A light Holo shade of orange -->
<color name="holo_orange_light">#ffffbb33</color>
<!-- A dark Holo shade of orange -->
<color name="holo_orange_dark">#ffff8800</color>
<!-- A really bright Holo shade of blue -->
<color name="holo_blue_bright">#ff00ddff</color>
</resources>
最后,是我们MainActivity中的示例
- package com.example.webviewrefresh;
- import android.os.Bundle;
- import android.app.Activity;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
- import android.view.Menu;
- import android.view.View;
- import android.webkit.WebChromeClient;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- public class MainActivity extends Activity {
- private SwipeRefreshLayout swipeLayout;
- private WebView webview;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- webview = (WebView) findViewById(R.id.webview);
- swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
- swipeLayout.setOnRefreshListener(new OnRefreshListener() {
- @Override
- public void onRefresh() {
- // TODO Auto-generated method stub
- webview.loadUrl(webview.getUrl());
- }
- });
- swipeLayout.setColorScheme(R.color.holo_blue_bright,
- R.color.holo_green_light, R.color.holo_orange_light,
- R.color.holo_red_light);
- webview.loadUrl("https://www.baidu.com/");
- webview.getSettings().setJavaScriptEnabled(true);
- webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
- webview.requestFocus();
- webview.setWebViewClient(new WebViewClient(){
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- view.loadUrl(url);
- return true;
- }
- });
- //设置进度条
- webview.setWebChromeClient(new WebChromeClient(){
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- if (newProgress == 100) {
- swipeLayout.setRefreshing(false);
- } else {
- if (!swipeLayout.isRefreshing())
- swipeLayout.setRefreshing(true);
- }
- super.onProgressChanged(view, newProgress);
- }
- });
- }
- }