允许和禁止viewpager仅根据某些条件在一个方向上滑动

时间:2014-01-17 13:05:40

标签: android

我在这个主题上搜索了很多,发现没有可行的解决方案。有人告诉我覆盖我的viewpager类,有些人告诉我把原来的viewpager类带到我的应用程序。我尝试了这两种方法,但都是徒劳的。我想在viewpager disable中保持向右滑动,直到特定条件为真。帮我解决我的问题。目前我正在重写我的onInterceptTouchEvent(),就像轰鸣声......

公共类CustomViewPagerWithNoRightSwipe扩展了ViewPager {

public CustomViewPagerWithNoRightSwipe(Context context) {
    super(context);
}

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



@Override
public boolean onTouchEvent(MotionEvent arg0) {
    // TODO Auto-generated method stub
    return super.onTouchEvent(arg0);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent touchevent) {
    // detect the swipe to right 

    float x1 = 0;
    float y1 = 0;
    float x2 = 0;
    float y2 = 0;
    switch (touchevent.getAction()) {
    // when user first touches the screen we get x and y coordinate
    case MotionEvent.ACTION_DOWN: {
        x1 = touchevent.getX();
        y1 = touchevent.getY();
        return true;
    }
    case MotionEvent.ACTION_UP: {
        x2 = touchevent.getX();
        y2 = touchevent.getY();

        // if left to right sweep event on screen
        if (x1 < x2) {
            if (ViewPagerData.flagTsetNavigation1
                    || ViewPagerData.flagTsetNavigation2
                    || ViewPagerData.flagTsetNavigation3)
                return true;
        }

        // if right to left sweep event on screen
        if (x1 > x2) {
            return true;

        }

    }
    }

    return false;
}

}

问题:  1)viewpager仍然向右滑动。  2)当我在我的xml中使用这个viewpager类时,我在viewpager页面(片段)中的childviews变得不可点击。

请解决我的问题。提前谢谢。

0 个答案:

没有答案
相关问题