Android 水平滚动视图的行为类似于 iPhone(分页)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2595063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:28:26  来源:igfitidea点击:

Android horizontal scrollview behave like iPhone (paging)

androidimagescrollpaging

提问by Davide Vosti

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 on a the iPhone equivalent of the HSW (scrolling the list should stop at every page on the list, not continue moving).

我在 Horizo​​ntalScrollView 中有一个 LinearLayout。内容只是一张图片。在滚动时,我需要实现与在 iPhone 等效的 HSW 上设置分页选项时获得的相同行为(滚动列表应在列表中的每个页面处停止,而不是继续移动)。

How is this done in Android? Should I implement this features by myself or there is a particular property to set or a subclass of HSV to implement?

这是如何在Android中完成的?我应该自己实现这个功能还是有一个特定的属性要设置或 HSV 的子类要实现?

采纳答案by Davide Vosti

So, my solution is:

所以,我的解决方案是:

  1. Intercept the onTouch event and calculate whether the page should change to the next or keep on the current
  2. Inherit from HorizontalScrollView and override the method computeScroll
  1. 拦截onTouch事件,计算页面是应该切换到下一个还是保持当前
  2. 从 Horizo​​ntalScrollView 继承并覆盖方法 computeScroll

The method computeScroll the called to move the list. By default I suppose it's implemented to decelerate with a certain ratio... Since I don't want this motion, I just override it without specifing a body.

调用computeScroll 方法来移动列表。默认情况下,我认为它实现了以一定比例减速......因为我不想要这个动作,我只是覆盖它而不指定身体。

The code for the event handler is:

事件处理程序的代码是:

_scrollView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP)
            {
                float currentPosition = _scrollView.getScrollX();
                float pagesCount = _horizontalBar.getChildCount();
                float pageLengthInPx = _horizontalBar.getMeasuredWidth()/pagesCount;
                float currentPage = currentPosition/pageLengthInPx;

                Boolean isBehindHalfScreen =  currentPage-(int)currentPage > 0.5;

                float edgePosition = 0;
                if(isBehindHalfScreen)
                {
                    edgePosition = (int)(currentPage+1)*pageLengthInPx;
                }
                else
                {
                    edgePosition = (int)currentPage*pageLengthInPx;
                }

                _scrollView.scrollTo((int)edgePosition, 0);
            }

            return false;
        }
    });

And in my inherited HorizontalScrollView

在我继承的 Horizo​​ntalScrollView 中

@Override
    public void  computeScroll  (){
        return;
    }

回答by aveyD

I came across a nice solution here:

我在这里遇到了一个很好的解决方案:

Horizontal Pager

水平寻呼机

this is a cleaned up GitHub version of the code found here:

这是此处找到的代码的清理 GitHub 版本:

Real View Switcher

实景切换器

It may seem like overkill for just using it on images, but this solution allows for infinite paging with using a little trick (ie: when on first page you can scroll back to last page and when on last page you can scroll forward to first page). It also allows you to have an unknown number of pages and dynamically generate content by using another little trick. Please see my comment in the second link here

仅在图像上使用它似乎有点矫枉过正,但是此解决方案允许使用一个小技巧进行无限分页(即:在第一页上时您可以回滚到最后一页,而在最后一页上时您可以向前滚动到第一页)。它还允许您拥有未知数量的页面并使用另一个小技巧动态生成内容。请在此处的第二个链接中查看我的评论

for details on how i accomplished this.

有关我如何完成此操作的详细信息。

Hope this helps.

希望这可以帮助。

回答by Patrick Boos

The new Compatibility Package (revision 3) in Android added a ViewPager which does that.

Android 中的新兼容性包(修订版 3)添加了一个 ViewPager 来执行此操作。

http://developer.android.com/sdk/compatibility-library.html

http://developer.android.com/sdk/compatibility-library.html

回答by David Hernandez

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;
        }