最近一直在整理以前做项目遇到的一些问题,今天正好项目需求要加这个,就给加上了,实现的过程中还遇到一些不大不小的坑,怕到时候忘了,就抽空记录下来,好了,闲话不多说,该进入正题了。
首先我自定义了一个webview类
public class SwipeWebView extends WebView {
private MySwipeRefreshLayout swipeRefreshLayout;
public SwipeWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setSwipeRefreshLayout(MySwipeRefreshLayout swipeRefreshLayout){
this.swipeRefreshLayout = swipeRefreshLayout;
setWebChromeClient(new WebChromeClient());
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (this.getScrollY() == 0){
swipeRefreshLayout.setEnabled(true);
}else {
swipeRefreshLayout.setEnabled(false);
}
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
//隐藏进度条
swipeRefreshLayout.setRefreshing(false);
} else {
if (!swipeRefreshLayout.isRefreshing())
swipeRefreshLayout.setRefreshing(true);
}
super.onProgressChanged(view, newProgress);
}
}
}
其中在重写这个自定义的类的时候还遇到一个问题,就是必须重写public SwipeWebView(Context context, AttributeSet attrs)这个构造方法,不然会报错Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android
然后就是xml了,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:fitsSystemWindows="true"
android:clipToPadding="true"
android:orientation="vertical">
<com.tdh.common.view.MySwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.tdh.rpms.ui.view.SwipeWebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.tdh.rpms.ui.view.SwipeWebView>
</com.tdh.common.view.MySwipeRefreshLayout>
</LinearLayout>
以下为java类里的代码;
webView.setSwipeRefreshLayout(swipeLayout);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webViewClient = new MyWebClient();
webView.setWebViewClient(webViewClient);
webView.loadUrl(url);
以上基本为全部代码,请笑纳。