2016-06-12 51 views
4

我將添加pull刷新我的web視圖,以便刷新我的webview。我看到這個頁面上的所有問題,但我不能找到很好的方式來增加拉刷新......添加pull刷新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> 

我希望任何人都可以幫助我解決這個問題。

回答

13

你可以在刷卡refesh佈局這樣

<?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> 

包裹的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(); 
    } 
    } 
    ); 

} 

} 
+0

當我粘貼webview.reload()它給了我一個錯誤在那一行。 ()) – Hessel

+0

你已經在onCreate() 'Webview webview =(WebView)findViewById(R.id.web_view)' 中定義webview,然後在onCreate中調用webview.loadUrl(「your url」)'來初始加載wbeview。 然後在監聽器中調用'webview.reload()'。 – jitinsharma

+0

對我不起作用... – Hessel

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

或者,如果你想要一個像鉻圖標或東西

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

+0

我可以刷新我與web視圖? – Hessel

+0

你可以在MotionEvent.ACTION _DOWN後再次將loadURL()加上一些查詢,並且如果需要更多類型的運動,請參閱https://developer.android.com/reference/android/view/MotionEvent。html –

+0

ll給我一個錯誤 – Hessel

2

我認爲最好的辦法是使用SwipeRefreshLayout,但它沒有NestedScrollView它作爲jitinsharma描述,因爲這個可能的問題:webview height grows indefinitely inside a 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> 

活動參數:

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

0123在活動

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

        } 
       });