Android的水平滚动视图的行为很像iPhone(分页) [英] Android horizontal scrollview behave like iPhone (paging)

查看:150
本文介绍了Android的水平滚动视图的行为很像iPhone(分页)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Horizo​​ntalScrollView内的LinearLayout。内容只是一个形象。滚动时,我需要实现设置在了iPhone相当于HSW的分页选项时,(滚动列表应该停止在每一页的名单上,没有继续移动),你会得到相同的行为。

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).

这是如何在Android中做了什么?我应该实现这个功能由自己或有一个特殊的属性来设置或HSV的子类来实现?

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?

推荐答案

所以,我的解决办法是:

So, my solution is:

  1. 拦截onTouch事件,并计算出该页面是否应该改变下或继续当前的
  2. 从Horizo​​ntalScrollView继承和覆盖的方法computeScroll

的方法,computeScroll被叫移动至列表。默认情况下我想这是实现减速带有一定的比例? 因为我不希望这项议案,我只是覆盖它没有specifing身体。

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.

在code事件处理程序是:

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

和我继承Horizo​​ntalScrollView

And in my inherited HorizontalScrollView

@Override
    public void  computeScroll  (){
        return;
    }

这篇关于Android的水平滚动视图的行为很像iPhone(分页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆