Horizo​​ntalScrollView内SwipeRefreshLayout [英] HorizontalScrollView inside SwipeRefreshLayout

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

问题描述

我实现了我的应用程序中的新的 SwipeRefreshLayout 组件,它可以与任何垂直意见,如的ListView 的GridView 滚动型

I implemented the new SwipeRefreshLayout component in my application and it works well with any vertical views, like ListView, GridView and ScrollView.

它的行为很不好,水平的观点,如 Horizo​​ntalScrollView 。 当滚动到右侧或左侧时, SwipeRefreshLayout 查看缓存的触感,$ P $接收它pvents的 Horizo​​ntalScrollView 并开始向垂直方向进行刷新。

It behaves very bad with horizontal views, like HorizontalScrollView. When scrolling to the right or left, the SwipeRefreshLayout view caches the touch, prevents the HorizontalScrollView from receiving it and starts scrolling vertically to perform the refresh.

我试图解决这个问题,因为我previously解决的问题与垂直滚动型 ViewPager 里面,用requestDisallowInterceptTouchEvent ,但没有奏效<$ C C $>。我还注意到,这种方法是在原 SwipeRefreshLayout 类,而不返回超级覆盖。谷歌的开发者给出了评语,而不是 //都能跟得上。:)

I tried solving this issue as I previously solved issues with vertical ScrollView with ViewPager inside, using requestDisallowInterceptTouchEvent but it didn't work. I also noticed that this method is overridden in the original SwipeRefreshLayout class without returning the super. Google's developer left a comment instead "//Nope." :)

由于 SwipeRefreshLayout 成分是比较新的,我无法找到一个解决方案,修复了水平滚动的问题,同时还允许刷卡刷新视图来跟踪和处理垂直滚动所以我想我会和希望它将不遗余力有人一两个小时分享我的解决方案。

Because SwipeRefreshLayout component is relatively new, I couldn't find a solution that fixes the horizontal scroll issue while still allowing the swipe to refresh view to track and handle vertical scrolling so I thought I'll share my solution with hopes it will spare someone an hour or two.

推荐答案

我解决它通过扩展 SwipeRefreshLayout 并覆盖其 onInterceptTouchEvent 。里面,本人计算若X距离用户已漫游大于触摸污液。如果是这样,则意味着用户刷卡水平,为此我回这让孩子视图( Horizo​​ntalScrollView 在这种情况下),以获得所述触摸事件

I solved it by extending SwipeRefreshLayout and overriding its onInterceptTouchEvent. Inside, I calculate if the X distance the user has wandered is bigger than the touch slop. If it does, it means the user is swiping horizontally, therefor I return false which lets the child view (the HorizontalScrollView in this case) to get the touch event.


public class CustomSwipeToRefresh extends SwipeRefreshLayout {

    private int mTouchSlop;
    private float mPrevX;

    public CustomSwipeToRefresh(Context context, AttributeSet attrs) {
        super(context, attrs);

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPrevX = MotionEvent.obtain(event).getX();
                break;

            case MotionEvent.ACTION_MOVE:
                final float eventX = event.getX();
                float xDiff = Math.abs(eventX - mPrevX);

                if (xDiff > mTouchSlop) {
                    return false;
                }
        }

        return super.onInterceptTouchEvent(event);
    }
}

这篇关于Horizo​​ntalScrollView内SwipeRefreshLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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