1

我有一个 ScrollView 的 ViewPager inisde,而 ViewPager 不能正常工作。我知道,这个问题很相似,我用谷歌搜索并阅读了一些解决方案。此外,在 ScrollView 后面我有一些堆叠的片段。我在 ScrollView 触摸处理(扩展 viewpager 和滚动视图)中尝试了这个 Horizo​​ntalScrollView,它工作得更好,但并不完美。我认为堆叠的片段正在捕获 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是正确的,问题是它们拦截事件的片段。

4

0 回答 0