里面ViewPager Horizo​​ntalScrollView行动 [英] Actions of HorizontalScrollView inside ViewPager

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

问题描述

我需要把里面Horizo​​ntalScrollView ViewPager。 ViewPager的每个孩子都是滚动型与Horizo​​ntalScrollView内。
我放大Horizo​​ntalScrollView的孩子。它后,我需要让用户滚动Horizo​​ntalScrollView而不是滚动到ViewPager的下一个页面。为了处理动作创建自定义ViewPager:

I need to place HorizontalScrollView inside ViewPager. Every child of ViewPager is ScrollView with HorizontalScrollView inside. I zoom child of HorizontalScrollView. After it I need to allow user to scroll HorizontalScrollView but not to scroll to the next page of ViewPager. To handle action I create custom ViewPager:

public class MyViewPager extends ViewPager {

    private static final String TAG = MyViewPager.class.getName();

    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            final int action = MotionEventCompat.getActionMasked(ev);

            // Always handle the case of the touch gesture being complete.
            if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
                return super.onInterceptTouchEvent(ev); // Do not intercept touch event, let the child handle it
            }

            switch (action) {
                case MotionEvent.ACTION_MOVE: {
                    ViewGroup view=(ViewGroup)(getChildAt(getCurrentItem()));
                    if (view.getChildCount()>0) {
                        view = (ViewGroup) view.getChildAt(0);
                        if (view instanceof CustomHorizontalScroll) {
                            CustomHorizontalScroll scroll = (CustomHorizontalScroll) view;
                            if (((CustomHorizontalScroll) view).isZoom()) {
                                return false;
                            } else {
                                return true;
                            }
                        }
                    }
                    break;
                }
                default: return super.onInterceptTouchEvent(ev);

            }

        }catch (Exception e){
            return super.onInterceptTouchEvent(ev);
        }
        return super.onInterceptTouchEvent(ev);
    }

}

问题是与onTouchAction。在我横看还有的onclick事件。如果我使用自定义ViewPager然后有时候的onClick不工作。磨我需要做什么?如何使用ActionDown在这种情况下工作?

The problem is with onTouchAction. In my horizontal view there are onClick events. If I use custom ViewPager then sometimes onClick dont work. Whet I need to do? How to work with ActionDown in this situation?

推荐答案

实施 onTouch 的onClick 会消耗通过 onTouch 触摸事件,并认为这是消费,而不是把它传递到其它各种触摸处理程序,使您能够 onClickListener 没用。
文档:

implementing onTouch and onClick will consume the touch event by onTouch and consider it consumed and not pass it on to the other various touch handlers which make your onClickListener useless. the Documentation:

onTouch() - 这将返回一个布尔值,表明你的听众是否消耗了本次活动。重要的是,这一事件可以有相互跟随的多个动作。所以,如果你在收到向下的动作时返回false,即表示您还没有消耗的事件,也没有兴趣从这个事件的后续操作。因此,你将不会被要求对事件中的任何其他操作,如手指的手势,或最终采取后续行动的事件。

onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.

下面是一些可用的解决方案来处理这种情况:

here is some of the available solutions to handle this situation:

1,你可以使用 onToach 的onClick 作为一个内部类,而不是实施每一个聆听者。

1-you can use onToach and onClick as an inner classes instead of implementing each listener.

2 - 您可以使用 onToach 来检测标签事件:

2- you can use onToach to detect a tab event:

if (gestureDetector.onTouchEvent(arg)) {
                // single tap
                return true;
            } else {
                // action up or down code 
            }

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

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