垂直视图寻呼机内的水平视图寻呼机。如何将事件从子视图寻呼机传递到父视图寻呼机

时间:2016-05-17 12:31:21

标签: android event-handling android-viewpager

我在垂直View Pager内有一个水平View Pager。所以水平视图寻呼机工作正常,因为它是一个孩子,它首先得到事件。但是当我在水平视图寻呼机上进行垂直滑动时,垂直视图寻呼机不会滑动(因为孩子消耗了该事件)。因此,如果事件是垂直事件,我需要进行解决,以便我可以将事件传递给父(垂直)视图寻呼机。

我已覆盖public boolean onTouchEvent(MotionEvent event)课程中的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>

0 个答案:

没有答案