SwipeRefreshLayout和Vertical ViewPager触摸问题 [英] SwipeRefreshLayout and Vertical ViewPager Touch Issues

查看:122
本文介绍了SwipeRefreshLayout和Vertical ViewPager触摸问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Custom SwipeRefreshLayout的Activity和一个垂直滑动的Vertical ViewPager.(基本上是一副纸牌,可以使用滑动刷新来刷新).

I have a Activity with Custom SwipeRefreshLayout and a Vertical ViewPager which swipes vertically. (basically a deck of cards which can be refershed by using swipe to refresh).

所以现在,当我尝试滑动Vertical Viewpager SwipeRefreshLayout 时,将处理触摸事件并刷新布局,并且视图寻呼机的页面不会移动.

So Now when i try to swipe the Vertical Viewpager SwipeRefreshLayout handles the touch event and refresh the layout and the view pager's page does't move.

我尝试了两件事但没有运气:

Two things i tried but with no luck:

  1. 仅当用户从工具栏. MotionEvent.ACTION_DOWN 事件 getY()给出工具栏的y协调.

  1. Enable swipe to refresh layout only when user starts from the toolbar. MotionEvent.ACTION_DOWN event getY() gives toolbar's y coordinate.

如果 ViewPager 是第一个接受触摸事件的人,则
不要将其委托给 SwipeRefreshLayout .

If the ViewPager is the first to accept the touch event then
don't delegate it to SwipeRefreshLayout.

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

The Layout file of the activity looks like this.

<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触摸拦截操作效果不佳

My CustomSwipeToRefresh touch intercept action which doesnot work well

 @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);
    }

推荐答案

以下逻辑对我有用

当用户开始从工具栏拖动时,启用滑动以刷新布局.即,如果用户尝试从顶部开始滑动并且包括工具栏,则仅启用滑动,并且如果用户在工具栏位置之后触摸开始,则禁用滑动

Enable swipe to refresh layout when user starts dragging from the toolbar. i.e if the user tries to swipe from very top and it includes the toolbar then only enable swipe and if user touch start after toolbar position then disable swipe

@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);
    }

这篇关于SwipeRefreshLayout和Vertical ViewPager触摸问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆