0

我有一个自定义SwipeRefreshLayout和垂直滑动垂直ViewPager活动。 (基本上是一副可以通过滑动刷新来刷新的卡片)。SwipeRefreshLayout和垂直ViewPager触摸问题

所以现在当我尝试滑动垂直ViewpagerSwipeRefreshLayout处理触摸事件并刷新布局和视图寻呼机的页面不会移动。

两件事情我试过,但没有运气:

  1. 启用刷卡,只有当用户从 工具栏开始刷新布局。 MotionEvent.ACTION_DOWN事件getY()给出了工具栏的坐标y 。

  2. 如果ViewPager是第一个接受触摸事件,然后
    不要将其委托给SwipeRefreshLayout

活动的布局文件如下所示。

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:HelveticaTextView="http://schemas.android.com/apk/res-auto" 
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:fab="http://schemas.android.com/apk/res-auto" 
android:id="@+id/DrawerLayout" android:layout_width="match_parent" 
android:layout_height="match_parent"> 


<com.CustomSwipeToRefresh xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/swipe_container" android:layout_width="match_parent" 
    android:layout_height="100dp" app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinatorLayout" android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/tool_bar" android:layout_width="match_parent" 
      android:layout_height="@dimen/toolbar_min_height" 
      android:background="@color/theme_color_yellow_dark" 
      app:layout_collapseMode="pin" 
      app:layout_scrollFlags="scroll|enterAlways" app:titleTextColor="@color/white"> 

     </android.support.v7.widget.Toolbar> 


     <RelativeLayout android:layout_width="match_parent" 
      android:layout_height="match_parent" android:layout_marginBottom="@dimen/toolbar_min_height" 
      android:layout_marginTop="@dimen/toolbar_min_height" 
      android:background="@color/transparent"> 

      <com.VerticalViewPager 
       android:id="@+id/pagerhome" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 

     </RelativeLayout> 

    </android.support.design.widget.CoordinatorLayout> 

</com.CustomSwipeToRefresh> 

<!-- to show nav drawer icons here --> 
<android.support.v7.widget.RecyclerView 
    android:id="@+id/RecyclerView" android:layout_width="320dp" 
    android:layout_height="match_parent" android:layout_gravity="left" 
    android:background="#ffffff" android:scrollbars="vertical"> 

    </android.support.v7.widget.RecyclerView> 


</android.support.v4.widget.DrawerLayout> 

我这亘古不变的工作做好

@Override 
    public boolean onInterceptTouchEvent(MotionEvent event) { 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 

       mPrevY = MotionEvent.obtain(event).getY(); 
       mDeclined = false; // New action 
       break; 

      case MotionEvent.ACTION_MOVE: 


       final float eventY = event.getY(); 
       float yDiff = Math.abs(eventY - mPrevY); 

       //disable swipe to refresh if user touches below 100px or dp 
       if (mDeclined || mPrevY > 100 ) { 
        mDeclined = true; 
        return false; 
       } 
     } 

     return super.onInterceptTouchEvent(event); 
    } 
+0

尝试打印DOWN的Y位置和MOVE,然后尝试应用条件。 – NehaK

+0

谢谢@NehaK。我刚刚解决了它。 –

回答

1

CustomSwipeToRefresh触摸拦截行动的逻辑之下为我工作

启用刷卡刷新布局时,用户开始从工具栏拖动。 即如果用户试图从最高层到刷卡,它包括工具栏,然后只允许刷卡,如果用户触摸工具栏的位置,然后关闭刷卡后开始enter image description here

@Override 
    public boolean onInterceptTouchEvent(MotionEvent event) { 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       mPrevY = MotionEvent.obtain(event).getY(); 
       mDeclined = false; 

       if (mPrevY > getResources().getDimensionPixelSize(R.dimen.disable_swipe_after)) { 
        mDeclined = true; 
       } 
       Log.d("Declined", mDeclined + "--" + mPrevY); 

       break; 

      case MotionEvent.ACTION_MOVE: 

       if (mDeclined) { 
        return false; 
       } 
     } 
     return super.onInterceptTouchEvent(event); 
    }