2016-02-28 100 views
0

我有一個SwipeRefreshLayout內的Webview,它工作正常。但只要我需要在webview中向上滾動,pull to refresh函數就會被調用。有什麼方法可以在webview中的特定區域被觸摸時禁用該功能?這是我的LayoutFile在特定區域禁用Android SwipeRefreshLayout

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android:android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/swipeRefreshLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 

     <WebView 
      android:id="@+id/webview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      /> 
    </android:android.support.v4.widget.SwipeRefreshLayout> 
</RelativeLayout> 

回答

0

您將需要爲web視圖創建一個觸摸監聽器。在那裏,如果觸摸事件座標位於目標區域內,則可以在您的父級(SwipeRefreshLayout)上調用requestDisallowInterceptTouchEvent。您需要在事件Up或取消時在父級上重新啓用觸摸事件。

類似下面的代碼框架(父母是你的SwipeRefreshLayout視圖):

switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     if (touch in target area) { 
      parent.requestDisallowInterceptTouchEvent(true); 
     } 
     break; 
    case MotionEvent.ACTION_CANCEL: 
    case MotionEvent.ACTION_UP: 
     if (disallowed parent) { 
      parent.requestDisallowInterceptTouchEvent(false); 
     } 
     break; 
    default: 
     break; 
    } 

你也可以嘗試在SwipeRefreshLayout視圖,而不是requestDisallowInterceptTouchEvent調用的setEnabled(假)。

您可以考慮的另一個選項是,只有當webview的滾動Y爲0時,才允許拉動刷新,因此如果您位於webview內容的頂部。當你開始向下滾動時,你會禁用你的父母(SwipeRefreshLayout),當你回滾到頂部(滾動Y == 0)時,你會重新啓用你的父母。