Java 在 webview 上添加 pull to refresh 以进行刷新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37775213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding pull to refresh on webview for refreshing
提问by Hessel
I will add pull to refresh on my webview so it refresh my webview. i have seen all questions on this page but i can't find the good way to add pull to refresh...
我将在我的 webview 上添加 pull to refresh,以便它刷新我的 webview。我已经看到了这个页面上的所有问题,但我找不到添加下拉刷新的好方法......
Mainactivity.java
主要活动.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
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>
I hope anyone can help me and solve this problem for me.
我希望任何人都可以帮助我并为我解决这个问题。
采纳答案by jitinsharma
You could wrap webview in Swipe refesh layout like this
您可以像这样在 Swipe refesh 布局中包装 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>
In java
在 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();
}
}
);
}
}
回答by Yash Jain
Try This if you want to just pull to refresh
如果您只想拉动刷新,请尝试此操作
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;
}
Or if you want an icon or something like chrome
或者如果你想要一个图标或类似 chrome 的东西
回答by Vaibhav
https://developer.android.com/training/swipe/add-swipe-interface.html
https://developer.android.com/training/swipe/add-swipe-interface.html
You can use the Swipe-to-Refresh Layout, it is easy to use and takes care of the refresh animation for you.
您可以使用 Swipe-to-Refresh Layout,它易于使用并为您处理刷新动画。
You can add an OnRefreshListener to refresh your webview
你可以添加一个 OnRefreshListener 来刷新你的 webview
回答by Mihuilk
I think the best solution is to use SwipeRefreshLayout
but without NestedScrollView
inside it as jitinsharmadescribed, beacause this possible problem : webview height grows indefinitely inside a nestedScrollView.But there is another solution for scrolling problem as described by vizZ,to implement ViewTreeObserver.OnScrollChangedListener
.So working solution shold be something like this:
我认为最好的解决办法是使用SwipeRefreshLayout
,但没有NestedScrollView
在它里面的jitinsharma描述,东阳这个可能出现的问题: 网页视图高度无限期nestedScrollView内增长。但有滚动问题的另一个解决方案是通过描述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 parameters:
活动参数:
private WebView mWebView;
private SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
on Activity onCreate
:
关于活动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();
}
}
);
and implements of activity:implements AdvancedWebView.Listener
和活动的实施:implements AdvancedWebView.Listener
on Activity onStop
:
关于活动onStop
:
mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
and Activity onStart
:
和活动onStart
:
mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mWebView.getScrollY() == 0)
mySwipeRefreshLayout.setEnabled(true);
else
mySwipeRefreshLayout.setEnabled(false);
}
});
回答by Rahul Suresh
This should work perfectly! In your layout file, wrap the webview in a SwipeRefreshLayout.
这应该可以完美运行!在您的布局文件中,将 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>
In your Java file, declare SwipeRefreshLayout mySwipeRefreshLayout;
outside the class.
Then, add this to the onCreate()
method
在您的 Java 文件中,SwipeRefreshLayout mySwipeRefreshLayout;
在类外声明 。然后,将其添加到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));
}
}
);
回答by Bhavsar1311
activity_main.xml
活动_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>
For Kotllin:
对于科特林:
MainActivity.kt
主活动.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
}
}