开发者

HorizontalScrollView paginated

开发者 https://www.devze.com 2023-02-19 18:18 出处:网络
I want to do a HorizontalScrollView paginated. If you move开发者_如何学编程 the screen to right, then it would show the right \"page\" and if you move the screen to left, then it would show the left \

I want to do a HorizontalScrollView paginated. If you move开发者_如何学编程 the screen to right, then it would show the right "page" and if you move the screen to left, then it would show the left "page".


I've done this thing in the past. You can do this via a custom touch listener:

public MyHorizontalScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL ){
                    int scrollX = getScrollX();
                    int itemWidth = getMeasuredWidth();
                    int activeItem = ((scrollX + itemWidth / 2) / itemWidth);
                    int scrollTo = activeItem * itemWidth;
                    smoothScrollTo(scrollTo, 0);

                    return true;
                } else {
                    return false;
                }
            }
        });
    }

Pretty self-explanatory, I think. This assumes that the width of your pages is constant and equal to the width of the whole scrollview.

0

精彩评论

暂无评论...
验证码 换一张
取 消