在webview上添加pull to refresh以进行刷新

我将在我的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

     

我希望有人能帮助我并为我解决这个问题。

您可以像这样在Swipe refesh布局中包装webview

         

在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但在jitinsharma描述中没有NestedScrollView ,因为这个可能的问题: webview高度无限期地在nestedScrollView内增长 。但是还有另一种解决方案,如vizZ所描述的滚动问题,实现ViewTreeObserver.OnScrollChangedListener 。所以工作解决方案应该是这样的:

      

活动参数:

 private WebView mWebView; private SwipeRefreshLayout mySwipeRefreshLayout; private ViewTreeObserver.OnScrollChangedListener mOnScrollChangedListener; 

on 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

on Activity 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); } }); 

https://developer.android.com/training/swipe/add-swipe-in​​terface.html

您可以使用“滑动到刷新”布局,它易于使用并为您处理刷新animation。

您可以添加OnRefreshListener来刷新Webview

如果您想要刷新,请尝试此操作

  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; } 

或者,如果你想要一个图标或类似铬的东西

https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh

这应该完美! 在布局文件中,将webview包装在SwipeRefreshLayout中。

       

在您的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)); } } );