SwipeRefreshLayout 是谷歌官方下拉刷新控件,4.0以下的版本需要用到 Android-support-v4.jar包才能用到
官网API地址:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
SwipeRefreshLayout 使用起来是非常简单的,只需要在可以滑动的控件外层添加即可,如:WebView、ListView和ScroolView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 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_baidu"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
常用方法:
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 FindFragment extends Fragment {
private WebView webview_baidu;
private SwipeRefreshLayout swipe_container;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.findfragment, null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
webview_baidu= (WebView) getActivity().findViewById(R.id.webview_baidu);
swipe_container= (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_container);
swipe_container.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webview_baidu.loadUrl(webview_baidu.getUrl());
}
});
webview_baidu.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
//重新加载webview网页
// webview_baidu.loadUrl("https://shop149047097.m.taobao.com");
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
// handler.cancel();// 默认的处理方式,WebView变成空白页
handler.proceed();// 接受所有网站的证书
// handleMessage(Message msg); 其他处理
}
});
//取消滚动条
webview_baidu.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
//触摸焦点起作用
webview_baidu.requestFocus();
//设置进度条
webview_baidu.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
//隐藏进度条
swipe_container.setRefreshing(false);
} else {
if (!swipe_container.isRefreshing())
swipe_container.setRefreshing(true);
}
super.onProgressChanged(view, newProgress);
}
});
webview_baidu.getSettings().setJavaScriptEnabled(true);
webview_baidu.getSettings().setSupportZoom(true);//支持对网页缩放
webview_baidu.getSettings().setBuiltInZoomControls(true);//支持android4.0
webview_baidu.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webview_baidu.getSettings().setDefaultFontSize(18);
//默认缩放模式
webview_baidu.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
// webview_baidu.loadUrl("http://www.cdqldz.com/");
webview_baidu.loadUrl("https://shop149047097.m.taobao.com");
}
}