2016-02-28 99 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)时,你会重新启用你的父母。