【发布时间】:2016-12-24 14:14:36
【问题描述】:
我有一个带有自定义 SwipeRefreshLayout 的 Activity 和一个垂直滑动的 Vertical ViewPager。 (基本上是一副牌,可以通过滑动来刷新)。
所以现在当我尝试滑动 Vertical Viewpager SwipeRefreshLayout 处理触摸事件并刷新布局时,视图寻呼机的页面不会移动。
我尝试了两件事但没有运气:
仅当用户从 工具栏。
MotionEvent.ACTION_DOWN事件getY()给出工具栏的 y 协调。如果
ViewPager是第一个接受触摸事件的则
不要委托给SwipeRefreshLayout。
activity 的 Layout 文件如下所示。
<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>
我的 CustomSwipeToRefresh 触摸拦截动作效果不佳
@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);
}
【问题讨论】:
-
尝试打印DOWN和MOVE的Y位置,然后尝试应用条件。
-
谢谢@NehaK。我刚刚解决了。
标签: android android-viewpager gesture ontouchlistener swiperefreshlayout