Horizontal view of the pager inside the vertical pager. How to transfer an event from a Child View Pager to a Parent View Pager

I have horizontal View Pagerinside vertical View Pager. Thus, horizontal viewing of a pager works fine because its a child and it receives the event first. But when I do a vertical scroll on the horizontal view of the pager, the vertical view of the pager does not scroll (because the child is consuming the event). Therefore, you need to have work so that I can pass the event to the parent (vertical) view of the pager, if the event is a vertical event.

I redefined the method public boolean onTouchEvent(MotionEvent event)in the class HorizontalViewPager: -

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch(event.getAction()) { // Check vertical and horizontal touches
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            downY = event.getY();
            return false;
        }
        case MotionEvent.ACTION_UP: {
            float upX = event.getX();
            float upY = event.getY();

            float deltaX = downX - upX;
            float deltaY = downY - upY;

            //HORIZONTAL SCROLL
            int min_distance = 10;
            if (Math.abs(deltaX) > Math.abs(deltaY)) {
                if (Math.abs(deltaX) > min_distance) {
                    // left or right
                    if (deltaX < 0) {
                        this.onLeftToRightSwipe();
                        return true;
                    }
                    if (deltaX > 0) {
                        this.onRightToLeftSwipe();
                        return true;
                    }
                } else {
                    //not long enough swipe...
                    return false;
                }
            }
            //VERTICAL SCROLL
            else {
                if (Math.abs(deltaY) > min_distance) {
                    // top or down
                    if (deltaY < 0) {
                        this.onTopToBottomSwipe();
                        return false;
                    }
                    if (deltaY > 0) {
                        this.onBottomToTopSwipe();
                        return false;
                    }
                } else {
                    //not long enough swipe...
                    return true;
                }
            }
            return true;
        }
    }
    return super.onTouchEvent(event);
}

parent_view_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainTabActivity">

    <com.android.boltt.customviews.VerticalViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />    
</RelativeLayout>

child_view_pager.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android.boltt.fragment.nutrition.BlankFragment">

    <com.android.boltt.customviews.CustomPagerContainer
        android:id="@+id/pager_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">

        <com.android.boltt.customviews.HorizontalViewPager
            android:layout_width="300dp"
            android:layout_height="400dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal" />

    </com.android.boltt.customviews.CustomPagerContainer>

</RelativeLayout>
+4

All Articles