WebView下拉刷新Demo

这里是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中的示例

  1. package com.example.webviewrefresh;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.support.v4.widget.SwipeRefreshLayout;  
  6. import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.webkit.WebChromeClient;  
  10. import android.webkit.WebView;  
  11. import android.webkit.WebViewClient;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     private SwipeRefreshLayout swipeLayout;  
  16.     private WebView webview;  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         webview = (WebView) findViewById(R.id.webview);  
  23.        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);  
  24.        swipeLayout.setOnRefreshListener(new OnRefreshListener() {  
  25.           
  26.         @Override  
  27.         public void onRefresh() {  
  28.             // TODO Auto-generated method stub  
  29.             webview.loadUrl(webview.getUrl());  
  30.         }  
  31.     });  
  32.           
  33.        swipeLayout.setColorScheme(R.color.holo_blue_bright,    
  34.                R.color.holo_green_light, R.color.holo_orange_light,    
  35.                R.color.holo_red_light);     
  36.           
  37.        webview.loadUrl("https://www.baidu.com/");    
  38.           
  39.        webview.getSettings().setJavaScriptEnabled(true);     
  40.          
  41.        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);    
  42.          
  43.        webview.requestFocus();    
  44.          
  45.        webview.setWebViewClient(new WebViewClient(){    
  46.            @Override    
  47.            public boolean shouldOverrideUrlLoading(WebView view, String url) {    
  48.                view.loadUrl(url);           
  49.                return true;           
  50.            }    
  51.        });    
  52.        //设置进度条    
  53.        webview.setWebChromeClient(new WebChromeClient(){    
  54.            @Override    
  55.            public void onProgressChanged(WebView view, int newProgress) {    
  56.                if (newProgress == 100) {    
  57.                     
  58.                    swipeLayout.setRefreshing(false);    
  59.                } else {    
  60.                    if (!swipeLayout.isRefreshing())    
  61.                        swipeLayout.setRefreshing(true);    
  62.                }    
  63.                    
  64.                super.onProgressChanged(view, newProgress);    
  65.            }    
  66.        });    
  67.           
  68.     }  
  69.   
  70.   
  71.     


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值