Android 下拉刷新控件SwipeRefreshLayout结合WebView使用

SwipeRefreshLayout 是谷歌官方下拉刷新控件,4.0以下的版本需要用到  android-support-v4.jar包才能用到

android-support-v4.jar 包下载地址:http://download.csdn.net/detail/h7870181/7784247

官网API地址:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

GitHub Demo下载地址: https://github.com/stormzhang/SwipeRefreshLayoutDemo



SwipeRefreshLayout 使用起来是非常简单的,只需要在可以滑动的控件外层添加即可,如:WebView、ListView和ScroolView.

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <android.support.v4.widget.SwipeRefreshLayout  
  7.         android:id="@+id/swipe_container"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent" >  
  10.   
  11.         <WebView  
  12.             android:id="@+id/webview"  
  13.             android:layout_width="match_parent"  
  14.             android:layout_height="match_parent"/>  
  15.           
  16.     </android.support.v4.widget.SwipeRefreshLayout>  
  17.   
  18. </FrameLayout>  


常用方法:

void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener)   设置刷新监听器

void setColorSchemeColors(int color1, int color2, int color3, int color4)  设置四种颜色进度条样式

void setRefreshing(boolean refreshing)  隐藏或显示进度条

boolean isRefreshing()  判断进度条是否显示 


结合WebView使用也挺简单的,可以实现一些功能,下拉刷新当前网页、点击网页在当前页面中浏览并显示SwipeRefreshLayout进度条,整体来说还是不错的

[java]  view plain  copy
 print ?
  1. public class Fragment5 extends Fragment {  
  2.     private View view;  
  3.     public WebView webview;  
  4.     private SwipeRefreshLayout swipeLayout;  
  5.       
  6.     @Override  
  7.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  8.             Bundle savedInstanceState) {  
  9.         view = inflater.inflate(R.layout.activity_fragment5, null);  
  10.           
  11.         swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);  
  12.         swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  
  13.               
  14.             @Override  
  15.             public void onRefresh() {  
  16.                 //重新刷新页面  
  17.                 webview.loadUrl(webview.getUrl());  
  18.             }  
  19.         });  
  20.         swipeLayout.setColorScheme(R.color.holo_blue_bright,  
  21.                 R.color.holo_green_light, R.color.holo_orange_light,  
  22.                 R.color.holo_red_light);  
  23.           
  24.         webview = (WebView)view.findViewById(R.id.webview);  
  25.           
  26.         webview.loadUrl("http://blog.csdn.net/h7870181");  
  27.         //添加javaScript支持  
  28.         webview.getSettings().setJavaScriptEnabled(true);   
  29.         //取消滚动条  
  30.         webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);  
  31.         //触摸焦点起作用  
  32.         webview.requestFocus();  
  33.         //点击链接继续在当前browser中响应   
  34.         webview.setWebViewClient(new WebViewClient(){  
  35.             @Override  
  36.             public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  37.                 view.loadUrl(url);         
  38.                 return true;         
  39.             }  
  40.         });  
  41.         //设置进度条  
  42.         webview.setWebChromeClient(new WebChromeClient(){  
  43.             @Override  
  44.             public void onProgressChanged(WebView view, int newProgress) {  
  45.                 if (newProgress == 100) {  
  46.                     //隐藏进度条  
  47.                     swipeLayout.setRefreshing(false);  
  48.                 } else {  
  49.                     if (!swipeLayout.isRefreshing())  
  50.                         swipeLayout.setRefreshing(true);  
  51.                 }  
  52.                   
  53.                 super.onProgressChanged(view, newProgress);  
  54.             }  
  55.         });  
  56.           
  57.         return view;  
  58.     }  
  59. }  

差点忘了贴出color.xml资源文件了,我呵了个呵!

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <!-- A light Holo shade of blue -->  
  5.     <color name="holo_blue_light">#ff33b5e5</color>  
  6.     <!-- A light Holo shade of green -->  
  7.     <color name="holo_green_light">#ff99cc00</color>  
  8.     <!-- A light Holo shade of red -->  
  9.     <color name="holo_red_light">#ffff4444</color>  
  10.     <!-- A dark Holo shade of blue -->  
  11.     <color name="holo_blue_dark">#ff0099cc</color>  
  12.     <!-- A dark Holo shade of green -->  
  13.     <color name="holo_green_dark">#ff669900</color>  
  14.     <!-- A dark Holo shade of red -->  
  15.     <color name="holo_red_dark">#ffcc0000</color>  
  16.     <!-- A Holo shade of purple -->  
  17.     <color name="holo_purple">#ffaa66cc</color>  
  18.     <!-- A light Holo shade of orange -->  
  19.     <color name="holo_orange_light">#ffffbb33</color>  
  20.     <!-- A dark Holo shade of orange -->  
  21.     <color name="holo_orange_dark">#ffff8800</color>  
  22.     <!-- A really bright Holo shade of blue -->  
  23.     <color name="holo_blue_bright">#ff00ddff</color>  
  24.   
  25. </resources>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值