ListView的滚动 - 一个接一个 [英] ListView scroll - one by one

查看:109
本文介绍了ListView的滚动 - 一个接一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须显示4个项目在一个时间一个ListView。我必须用一个滚动一个项目,之一。

I have a ListView that must display 4 items at a time. And I must scroll one item, one by one.

用户滚动ListView控件后,我必须重新调整滚动,以适应4个项目。我的意思是,我不能表现出了一半的项目。

After user scrolls the ListView, I must readjust the scroll to fit 4 items. I mean, I can´t show an item by half.

另外一个问题,有没有什么办法让当前的ListView scrollY偏移?因为listView.getScrollY()方法是查看但不能在ListView内滚轮的对象。

Another question, is there any way to get the current ListView scrollY offset? Because the listView.getScrollY() method is from View, but not the Scroller object inside the ListView.

推荐答案

我已经找到一个很好的解决方案。它的工作原理非常适合我。
可能是我的回答将帮助别人。

I've found an excellent solution. It works perfect for me. May be my answer will help someone.

class ScrollListener implements AbsListView.OnScrollListener{
        boolean aligned;

        @Override
        public void onScrollStateChanged(AbsListView absListView, int state) {
            if (state == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                aligned = false;
            }
            if (state == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                if (!aligned) {
                    if (Math.abs(absListView.getChildAt(0).getY()) < Math.abs(absListView.getChildAt(1).getY())) {
                        listView.smoothScrollToPosition(absListView.getFirstVisiblePosition());
                    } else {
                        listView.smoothScrollToPosition(absListView.getLastVisiblePosition());
                    }
                }
                aligned = true;
            }
        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }
    }

我必须说,在我的情况我有两个明显的项目,因此,如果你有4个,你应该使用索引玩(getChildAt(0)和getChildAt(1))
祝你好运!

I must say that in my situation I had two visible items so if you have 4 you should play with indexes ("getChildAt(0)" and "getChildAt(1)"). Good luck!

这篇关于ListView的滚动 - 一个接一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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