WebView下拉刷新Demo

本文介绍了一个使用SwipeRefreshLayout实现WebView页面加载进度显示及下拉刷新功能的示例。通过设置WebView的各种属性,如JavaScript支持、滚动条样式等,并利用SwipeRefreshLayout进行加载进度提示和刷新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里是xml布局文件


<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文件,用来展示进度条颜色


<?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);  
           }  
       });  
        
    }


  
}


好了,接下里你可以试一试了,希望有所帮助


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值