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.




    

        
        
    


常用方法:

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

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

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

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


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

public class Fragment5 extends Fragment {
	private View view;
	public WebView webview;
	private SwipeRefreshLayout swipeLayout;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.activity_fragment5, null);
		
		swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
		swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				//重新刷新页面
				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 = (WebView)view.findViewById(R.id.webview);
		
		webview.loadUrl("http://blog.csdn.net/h7870181");
		//添加javaScript支持
		webview.getSettings().setJavaScriptEnabled(true); 
		//取消滚动条
		webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
		//触摸焦点起作用
		webview.requestFocus();
		//点击链接继续在当前browser中响应 
		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);
			}
		});
		
		return view;
	}
}

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




    
    #ff33b5e5
    
    #ff99cc00
    
    #ffff4444
    
    #ff0099cc
    
    #ff669900
    
    #ffcc0000
    
    #ffaa66cc
    
    #ffffbb33
    
    #ffff8800
    
    #ff00ddff



没多大技术含量,纯属积累学习,还望大家见谅!


成功源于不断的学习和积累 !




你可能感兴趣的:(Android,学习)