一个带有一个片段的定向视图寻呼机

时间:2017-06-09 07:41:35

标签: android android-fragments android-viewpager fragment

我正面临一个问题我在视图寻呼机中有一个片段我需要在用户从右向左滑动时更新,幻灯片动画应该有一个片段 ,这里有一些我正在尝试的代码,事情是我也在片段内部查看寻呼机。

任何建议代码帮助将不胜感激。

这里是我为一个方向视图寻呼机尝试的一些代码

public class CustomViewPager extends ViewPager {

    private float initialXValue;
    private SwipeDirection direction;

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.direction = SwipeDirection.all;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.IsSwipeAllowed(event)) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.IsSwipeAllowed(event)) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    private boolean IsSwipeAllowed(MotionEvent event) {
        if (this.direction == SwipeDirection.all) return true;

        if (direction == SwipeDirection.none)//disable any swipe
            return false;

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            initialXValue = event.getX();
            return true;
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            try {
                float diffX = event.getX() - initialXValue;
                if (diffX > 0 && direction == SwipeDirection.right) {
                    // swipe from left to right detected
                    return false;
                } else if (diffX < 0 && direction == SwipeDirection.left) {
                    // swipe from right to left detected
                    return false;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        return true;
    }

    public void setAllowedSwipeDirection(SwipeDirection direction) {
        this.direction = direction;
    }
} 

2 个答案:

答案 0 :(得分:0)

你能解释一下吗?我仍然不明白,查看寻呼机里面只有一个片段?以及如何用1个片段滑动?

public class NoSwipeViewPager extends ViewPager {
    private float initialXValue;
    private SwipeDirection direction;

    public NoSwipeViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.direction = SwipeDirection.all;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (this.IsSwipeAllowed(event)) {
            return super.onTouchEvent(event);
        }

        return false;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        if (this.IsSwipeAllowed(event)) {
            return super.onInterceptTouchEvent(event);
        }

        return false;
    }

    public enum SwipeDirection {
        all, left, right, none;
    }

    private boolean IsSwipeAllowed(MotionEvent event) {
        if (this.direction == SwipeDirection.all) return true;

        if (direction == SwipeDirection.none)
            return false;

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            initialXValue = event.getX();
            return true;
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            try {
                float diffX = event.getX() - initialXValue;
                if (diffX > 0 && direction == SwipeDirection.right) {
                    return false;
                } else if (diffX < 0 && direction == SwipeDirection.left) {
                    return false;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        return true;
    }

    public void setAllowedSwipeDirection(SwipeDirection direction) {
        this.direction = direction;
    }
}

当你使用时:

viewPager.setAllowedSwipeDirection(NoSwipeViewPager.SwipeDirection.left);

答案 1 :(得分:0)

您可以尝试使用ViewPager的PageTransformer。这里是代码示例:

mViewPager.setPageTransformer(false, new FadePageTransformer());

/ **          *页面转换将DPAN隐藏在下一张部分可见的卡片中。* /

    private class FadePageTransformer implements ViewPager.PageTransformer {

            public void transformPage(View view, float position) {
   // you can do your work when page is transform
            }
        }

希望会有所帮助。