0
我有一個自定義SwipeRefreshLayout和垂直滑動垂直ViewPager活動。 (基本上是一副可以通過滑動刷新來刷新的卡片)。SwipeRefreshLayout和垂直ViewPager觸摸問題
所以現在當我嘗試滑動垂直Viewpager
SwipeRefreshLayout
處理觸摸事件並刷新佈局和視圖尋呼機的頁面不會移動。
兩件事情我試過,但沒有運氣:
啓用刷卡,只有當用戶從 工具欄開始刷新佈局。
MotionEvent.ACTION_DOWN
事件getY()
給出了工具欄的座標y 。如果
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);
}
嘗試打印DOWN的Y位置和MOVE,然後嘗試應用條件。 – NehaK
謝謝@NehaK。我剛剛解決了它。 –