scrollview内的ViewPager不起作用

时间:2012-10-15 22:05:32

标签: android touch scrollview android-viewpager fragment

我有一个ScrollView的ViewPager inisde,并且ViewPager无法正常工作。我知道,这个问题很相似,我用Google搜索并阅读了一些解决方案。此外,在ScrollView的后面我有一些堆叠的碎片。我尝试了这个HorizontalScrollView within ScrollView Touch Handling(扩展了viewpager和scrollview)并且效果更好,但并不完美。我认为堆叠的片段正在捕获Touch事件。

有人有这个问题吗?感谢

编辑:我添加了代码。

自定义滚动视图:

public class ScrollViewS extends ScrollView {
    /* THIS COMMENT CODE IS THE OTHER SOLUTION
    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;

    public ScrollViewS(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

    // Return false if we're scrolling in the x direction  
    class YScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if(Math.abs(distanceY) > Math.abs(distanceX)) {
                return true;
            }
            return false;
        }
    }
    */
    private float xDistance, yDistance, lastX, lastY;

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

    @Override
    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(xDistance > yDistance)
                    return false;
        }

        return super.onInterceptTouchEvent(ev);
    }
}

ViewPager代码:

public class ViewPagerS extends ViewPager {

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

    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Tell our parent to stop intercepting our events!
        boolean ret = super.onInterceptTouchEvent(ev);
        if (ret) {
            getParent().requestDisallowInterceptTouchEvent(true);
        }
        return ret;
    }
}

XML就是这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fondoapp"
    android:orientation="vertical" >


    <com.geo.sov.customviews.ScrollViewS
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/borde_redondeado"
        android:id="@+id/scrollview_fragmentaccounts"
         >

       <LinearLayout
            android:id="@+id/x"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="5dp"
            android:orientation="vertical" 
        >

            <TextView
                android:id="@+id/title_accounts"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textSize="14dp"
                android:textStyle="bold" />

            <com.geo.sov.customviews.ViewPagerS
                android:id="@+id/pager"
                android:layout_width="wrap_content"
                android:layout_height="95dp"
                android:background="#FFFFFFFF" /> 

            <com.viewpagerindicator.CirclePageIndicator
                android:id="@+id/indicator"
                android:layout_width="fill_parent"
                android:layout_height="20dp"
                android:padding="10dip"
                app:pageColor="#88FF0000"
                app:radius="3dp"
                app:strokeColor="#FF666666"
                app:strokeWidth="1dp" />

(........)

当我启动我的应用程序时,我有一个片段,如果用户正确登录,则使用以下代码进行更改:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Fragment f = new AccountsFragment();

fragmentTransaction.add(R.id.fragment, f);
fragmentTransaction.commit();  

我用碎片试验了一些问题。当我更改为AccountsFragments时,如果我触摸一个空白区域,触摸事件将传递给登录片段。我认为scrollview和viewpager是正确的,问题是他们拦截事件的片段。

0 个答案:

没有答案