我一直在尝试将SwipeRefreshLayout
与WebView
一起使用。为了分隔滚动触发器(用于垂直滚动页面)和SwipeRefreshLayout
,我尝试使用ViewTreeObserver
来检查页面滚动是否仅在顶部,然后SwipeRefreshTrigger
被调用,否则用户应该能够在页面中上下滚动。当应用程序启动时,一切都很好,用户可以在页面上上下滚动,并且SwipeRefreshLayout
仅在用户在页面顶部时尝试时才触发。用户关闭应用程序,应用程序开始在后台运行。之后,当用户再次打开应用程序时,WebView
布局会显示最后一个打开的页面。然后,用户无法滚动页面(仅SwipeRefreshLayout起作用)。有时,即使在某些URL更改或在页面中打开某些菜单之后,也会发生这种情况(如果需要,可以使用应用程序显示响应式网页)。
MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
SwipeRefreshLayout mySwipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipeContainer);
String URL = "www.mysite.com";
mWebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = mWebView.getSettings();
mWebView.loadUrl(URL);
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mWebView.getScrollY() == 0) {
mySwipeRefreshLayout.setEnabled(true);
} else {
mySwipeRefreshLayout.setEnabled(false);
}
}
});
// I override my URL loading, if this matters.
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:"))
{
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
view.loadUrl(url);
return true;
}
});
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mWebView.reload();
if (null != mySwipeRefreshLayout) {
mySwipeRefreshLayout.setRefreshing(false);
}
}
}
);
}
@Override
public void onStart() {
super.onStart();
// I added ViewTreeObserver here too, but no luck by doing this
mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mWebView.getScrollY() == 0)
mySwipeRefreshLayout.setEnabled(true);
else
mySwipeRefreshLayout.setEnabled(false);
}
});
}
@Override
public void onStop() {
mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
super.onStop();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fillViewport="true">
<WebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>