scrollview中的ViewPager停止父滚动视图滚动效果 - Android

时间:2015-02-10 11:11:01

标签: android android-viewpager android-scrollview android-viewgroup

在我的Android应用程序中,ViewPager内有ScrollView,但Scrollview无法滚动,并且viewpager内容无法正常显示。我提到很多帖子但没有得到任何解决方案。我提到了this link和许多其他帖子。请帮我解决这个问题。 谢谢。

代码:布局文件

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical" >

        <com.classes.ScrollViewForNesting
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/main_content_wide"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/black"
            android:fillViewport="true" >

            <RelativeLayout
                android:id="@+id/details_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#000"
                android:clipToPadding="false"
                android:orientation="vertical"
                android:paddingBottom="48dp"
                android:paddingRight="16dp"
                android:paddingTop="16dp" >

                <android.support.v4.view.ViewPager
                    android:id="@+id/newsPager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <android.support.v4.view.PagerTabStrip
                        android:id="@+id/pager_title_strip"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="top"
                        android:background="@color/tab_bg_color"
                        android:paddingBottom="10dp"
                        android:textColor="@android:color/white"
                        android:textSize="@dimen/news_tab_text_size" />
                </android.support.v4.view.ViewPager>
            </RelativeLayout>

        </com.classes.ScrollViewForNesting>
       <include layout="@layout/toolbar" />
    </FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>

自定义ScrollView代码是:

public class ScrollViewForNesting extends ScrollView {
private final int DIRECTION_VERTICAL = 0;
private final int DIRECTION_HORIZONTAL = 1;
private final int DIRECTION_NO_VALUE = -1;

private final int mTouchSlop;
private int mGestureDirection;

private float mDistanceX;
private float mDistanceY;
private float mLastX;
private float mLastY;
private float xDistance, yDistance, lastX, lastY;

int lastEvent = -1;

boolean isLastEventIntercepted = false;

public ScrollViewForNesting(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);

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

public ScrollViewForNesting(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ScrollViewForNesting(Context context) {
    this(context, null);
}

public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        xDistance = yDistance = 0f;
        lastX = ev.getX();
        lastY = ev.getY();

        break;

    case MotionEvent.ACTION_MOVE:
        final float curX = ev.getX();
        final float curY = ev.getY();
        xDistance += Math.abs(curX - lastX);
        yDistance += Math.abs(curY - lastY);
        lastX = curX;
        lastY = curY;

        if (isLastEventIntercepted && lastEvent == MotionEvent.ACTION_MOVE) {
            return false;
        }

        if (xDistance > yDistance) {

            isLastEventIntercepted = true;
            lastEvent = MotionEvent.ACTION_MOVE;
            return false;
        }

    }

    lastEvent = ev.getAction();

    isLastEventIntercepted = false;
    return super.onInterceptTouchEvent(ev);

}

private boolean shouldIntercept() {
    if ((mDistanceY > mTouchSlop || mDistanceX > mTouchSlop)
            && mGestureDirection == DIRECTION_NO_VALUE) {
        if (Math.abs(mDistanceY) > Math.abs(mDistanceX)) {
            mGestureDirection = DIRECTION_VERTICAL;
        } else {
            mGestureDirection = DIRECTION_HORIZONTAL;
        }
    }

    if (mGestureDirection == DIRECTION_VERTICAL) {
        return true;
    } else {
        return false;
    }
}

}

0 个答案:

没有答案