我将在我的webview上添加pull to refresh,以便刷新我的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>
我希望有人能帮助我并为我解决这个问题。
答案 0 :(得分:23)
您可以像这样
在Swipe refesh布局中包装webviewpackage 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();
}
}
);
}
}
在java中
Class
答案 1 :(得分:8)
我认为最好的解决方案是使用SwipeRefreshLayout
,但NestedScrollView
中没有ViewTreeObserver.OnScrollChangedListener
,因为这可能存在问题:jitinsharma。但还有另一个解决方案webview height grows indefinitely inside a nestedScrollView描述的滚动问题,以实现<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>
。所以工作解决方案应该是这样的:
private WebView mWebView;
private SwipeRefreshLayout mySwipeRefreshLayout;
private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener;
活动参数:
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();
}
}
);
:
implements AdvancedWebView.Listener
和活动的实现:onStop
活动mySwipeRefreshLayout.getViewTreeObserver().removeOnScrollChangedListener(mOnScrollChangedListener);
:
onStart
和活动 mySwipeRefreshLayout.getViewTreeObserver().addOnScrollChangedListener(mOnScrollChangedListener =
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (mWebView.getScrollY() == 0)
mySwipeRefreshLayout.setEnabled(true);
else
mySwipeRefreshLayout.setEnabled(false);
}
});
:
{{1}}
答案 2 :(得分:1)
https://developer.android.com/training/swipe/add-swipe-interface.html
您可以使用“滑动到刷新布局”,它易于使用,并为您处理刷新动画。
您可以添加OnRefreshListener来刷新您的webview
答案 3 :(得分:0)
如果您只想拉动刷新
,请尝试此操作 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;
}
或者,如果你想要一个图标或类似铬的东西
答案 4 :(得分:0)
这应该完美!在布局文件中,将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;
课外。
然后,将其添加到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));
}
}
);
答案 5 :(得分:0)
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
}
}