Android horizontal scrollview behave like iPhone (paging)

前端 未结 4 920
星月不相逢
星月不相逢 2020-12-02 06:15

I have a LinearLayout inside a HorizontalScrollView. The content is just a image. While scrolling, I need to achieve the same behavior you get when setting the paging option

4条回答
  •  眼角桃花
    2020-12-02 06:42

    i found another way to get the same effect and i think is more readable. Here is the way:

    @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP)
                {
                    float currentPosition = hsv.getScrollX();
                    float pagesCount = hsv.getChildCount();
                    float pageLengthInPx = hsv.getMeasuredWidth()/pagesCount;
    
                    int page = (int) (Math.floor((currentPosition - pageLengthInPx / 2) / pageLengthInPx) + 1);
                    hsv.scrollTo((int) (page * pageLengthInPx), 0);
                }
    
                return false;
            }
    

提交回复
热议问题