Actions of HorizontalScrollView inside ViewPager

741 views Asked by At

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

}

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?

1

There are 1 answers

0
A.S On BEST ANSWER

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() - 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-you can use onToach and onClick as an inner classes instead of implementing each listener.

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

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