在 webview 上添加 pull to refresh 以进行刷新
Adding pull to refresh on webview for refreshing
我将在我的 webview 上添加拉动刷新,以便它刷新我的 webview。我已经看到此页面上的所有问题,但我找不到添加拉动刷新的好方法...
Mainactivity.java
package com.vvhvb.hesselfeenstra.vvheerenveenseboys;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url ="http://heerenveenseboys.nl/";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
view.setWebViewClient(new WebViewClient());
view.loadUrl(url);
}
}
content_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity"
tools:showIn="@layout/activity_main">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
我希望任何人都可以帮助我并为我解决这个问题。
如果你只想拉动刷新,试试这个
ll = (LinearLayout)findViewById(R.id.CategorySelect_lLayout_noID);
ll.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(CategorySelect.this, "action DOWN called", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
return true;
}
或者如果你想要一个图标或类似的东西 chrome
https://developer.android.com/training/swipe/add-swipe-interface.html
您可以使用滑动刷新布局,它易于使用并为您处理刷新动画。
您可以添加一个 OnRefreshListener 来刷新您的 webview
您可以像这样将 webview 包装在滑动刷新布局中
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity"
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">
<WebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
在java
package com.vvhvb.hesselfeenstra.vvheerenveenseboys;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView view;
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 ="http://heerenveenseboys.nl/";
view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
view.setWebViewClient(new WebViewClient());
view.loadUrl(url);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
view.reload();
}
}
);
}
}
我认为最好的解决方案是使用 SwipeRefreshLayout
但不使用 NestedScrollView
作为 described, beacause this possible problem : .But there is another solution for scrolling problem as described by vizZ 来实现 ViewTreeObserver.OnScrollChangedListener
。所以工作解决方案应该是这样的:
<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">
<RelativeLayout
android:id="@+id/nonVideoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:windowSoftInputMode="adjustResize">
<WebView
android:id="@+id/main_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:windowSoftInputMode="adjustResize"
app:layout_constraintDimensionRatio="1" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Activity 参数:
private WebView mWebView;
private SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
在 Activity onCreate
:
mWebView = (WebView) findViewById(R.id.main_web_view);
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mWebView.reload();
}
}
);
和 activity 的实现:implements AdvancedWebView.Listener
于 Activity onStop
:
mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
和Activity onStart
:
mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mWebView.getScrollY() == 0)
mySwipeRefreshLayout.setEnabled(true);
else
mySwipeRefreshLayout.setEnabled(false);
}
});
这应该可以完美运行!在布局文件中,将 webview 包装在 SwipeRefreshLayout 中。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/webView"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
在您的 Java 文件中,声明 SwipeRefreshLayout mySwipeRefreshLayout;
class 之外。
然后,将其添加到 onCreate()
方法
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mWebview.reload();
mWebview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
// This is important as it forces webview to load from the instead of reloading from cache
mWebview .loadUrl(getString(R.string.app_link));
}
}
);
activity_main.xml
<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">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
对于 Kotllin:
MainActivity.kt
var mySwipeRefreshLayout: SwipeRefreshLayout? = null
var myWebView: WebView? = null
mySwipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipeContainer)
mySwipeRefreshLayout?.setOnRefreshListener {
Log.i("on refresh", "onRefresh called from SwipeRefreshLayout")
// This method performs the actual data-refresh operation.
// The method calls setRefreshing(false) when it's finished.
myWebView?.reload();
}
myWebView!!.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl("http://www.google.com")
return false;
}
override fun onPageFinished(view: WebView?, url: String?) {
mySwipeRefreshLayout?.isRefreshing = false
}
}
在研究过程中,我发现可以通过三种简单的方法在 Android 应用程序中实现下拉刷新:
- 您可以使用第三方库/SDK
或
- 您可以使用自定义实现(例如此处提到的代码片段)。此选项更复杂,但为自定义提供了更大的灵活性
或
- 在 Android WebViewGold 中,在 Config.java 内,只需将 ENABLE__PULL_REFRESH 设为 true
滚动愉快!
这是对我有用的代码部分。它处理两个问题:
当用户在 web 视图中滚动并且比用户再次尝试向上滚动时屏幕被刷新。但是,在 onStart() 和 onStop() 中添加代码会处理此问题,它会检查用户是否在 webview 中滚动,而不是禁用刷新部分。
使用 WebViewClient() 调用 onPageFinished() 会禁用刷新图标,否则它将进入永久刷新循环。
webView = findViewById(R.id.webview);
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
mySwipeRefreshLayout.setRefreshing(false);
}
});
webView.loadUrl("https://www.enter your url here");
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
}
@Override
protected void onStart() {
super.onStart();
mySwipeRefreshLayout.getViewTreeObserver()
.addOnScrollChangedListener(mOnScrollChangedListener =
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (webView.getScrollY() == 0)
mySwipeRefreshLayout.setEnabled(true);
else
mySwipeRefreshLayout.setEnabled(false);
}
});
}
@Override
protected void onStop() {
super.onStop();
mySwipeRefreshLayout.getViewTreeObserver()
.removeOnScrollChangedListener(mOnScrollChangedListener);
}
我将在我的 webview 上添加拉动刷新,以便它刷新我的 webview。我已经看到此页面上的所有问题,但我找不到添加拉动刷新的好方法...
Mainactivity.java
package com.vvhvb.hesselfeenstra.vvheerenveenseboys;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url ="http://heerenveenseboys.nl/";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
view.setWebViewClient(new WebViewClient());
view.loadUrl(url);
}
}
content_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity"
tools:showIn="@layout/activity_main">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
我希望任何人都可以帮助我并为我解决这个问题。
如果你只想拉动刷新,试试这个
ll = (LinearLayout)findViewById(R.id.CategorySelect_lLayout_noID);
ll.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(CategorySelect.this, "action DOWN called", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
return true;
}
或者如果你想要一个图标或类似的东西 chrome
https://developer.android.com/training/swipe/add-swipe-interface.html
您可以使用滑动刷新布局,它易于使用并为您处理刷新动画。
您可以添加一个 OnRefreshListener 来刷新您的 webview
您可以像这样将 webview 包装在滑动刷新布局中
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity"
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">
<WebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
在java
package com.vvhvb.hesselfeenstra.vvheerenveenseboys;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView view;
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 ="http://heerenveenseboys.nl/";
view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
view.setWebViewClient(new WebViewClient());
view.loadUrl(url);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
view.reload();
}
}
);
}
}
我认为最好的解决方案是使用 SwipeRefreshLayout
但不使用 NestedScrollView
作为 ViewTreeObserver.OnScrollChangedListener
。所以工作解决方案应该是这样的:
<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">
<RelativeLayout
android:id="@+id/nonVideoLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:windowSoftInputMode="adjustResize">
<WebView
android:id="@+id/main_web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:windowSoftInputMode="adjustResize"
app:layout_constraintDimensionRatio="1" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Activity 参数:
private WebView mWebView;
private SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
在 Activity onCreate
:
mWebView = (WebView) findViewById(R.id.main_web_view);
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mWebView.reload();
}
}
);
和 activity 的实现:implements AdvancedWebView.Listener
于 Activity onStop
:
mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
和Activity onStart
:
mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mWebView.getScrollY() == 0)
mySwipeRefreshLayout.setEnabled(true);
else
mySwipeRefreshLayout.setEnabled(false);
}
});
这应该可以完美运行!在布局文件中,将 webview 包装在 SwipeRefreshLayout 中。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/webView"
/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
在您的 Java 文件中,声明 SwipeRefreshLayout mySwipeRefreshLayout;
class 之外。
然后,将其添加到 onCreate()
方法
mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mWebview.reload();
mWebview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
// This is important as it forces webview to load from the instead of reloading from cache
mWebview .loadUrl(getString(R.string.app_link));
}
}
);
activity_main.xml
<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">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
对于 Kotllin:
MainActivity.kt
var mySwipeRefreshLayout: SwipeRefreshLayout? = null
var myWebView: WebView? = null
mySwipeRefreshLayout = findViewById<SwipeRefreshLayout>(R.id.swipeContainer)
mySwipeRefreshLayout?.setOnRefreshListener {
Log.i("on refresh", "onRefresh called from SwipeRefreshLayout")
// This method performs the actual data-refresh operation.
// The method calls setRefreshing(false) when it's finished.
myWebView?.reload();
}
myWebView!!.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl("http://www.google.com")
return false;
}
override fun onPageFinished(view: WebView?, url: String?) {
mySwipeRefreshLayout?.isRefreshing = false
}
}
在研究过程中,我发现可以通过三种简单的方法在 Android 应用程序中实现下拉刷新:
- 您可以使用第三方库/SDK 或
- 您可以使用自定义实现(例如此处提到的代码片段)。此选项更复杂,但为自定义提供了更大的灵活性 或
- 在 Android WebViewGold 中,在 Config.java 内,只需将 ENABLE__PULL_REFRESH 设为 true
滚动愉快!
这是对我有用的代码部分。它处理两个问题:
当用户在 web 视图中滚动并且比用户再次尝试向上滚动时屏幕被刷新。但是,在 onStart() 和 onStop() 中添加代码会处理此问题,它会检查用户是否在 webview 中滚动,而不是禁用刷新部分。
使用 WebViewClient() 调用 onPageFinished() 会禁用刷新图标,否则它将进入永久刷新循环。
webView = findViewById(R.id.webview); mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setDisplayZoomControls(true); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { mySwipeRefreshLayout.setRefreshing(false); } }); webView.loadUrl("https://www.enter your url here"); mySwipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { webView.reload(); } }); } @Override protected void onStart() { super.onStart(); mySwipeRefreshLayout.getViewTreeObserver() .addOnScrollChangedListener(mOnScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { if (webView.getScrollY() == 0) mySwipeRefreshLayout.setEnabled(true); else mySwipeRefreshLayout.setEnabled(false); } }); } @Override protected void onStop() { super.onStop(); mySwipeRefreshLayout.getViewTreeObserver() .removeOnScrollChangedListener(mOnScrollChangedListener); }