onScrolling监听器绑定到Android的ListView [英] Binding onScrolling listener to the android ListView

查看:132
本文介绍了onScrolling监听器绑定到Android的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想问问有没有一种可能性,一些监听器绑定到ListView滚动事件。

just wanted to ask is there a possibility to bind some listener to the ListView scroll event.

我想实现的是,一旦ListView控件已滚动至底部我想问一下服务器,更多的数据,如果任何在那里。

What I would like to achieve is that once ListView have been scrolled to the bottom I would like to ask server for more data if any is there.

我知道,有一个功能,如:?getLastVisiblePosition(),但我认为它必须BU使用某种监听器,以便与目前最后一个可见的位置进行比较,我说的对

I know that there is a function like: getLastVisiblePosition() but I assume it must bu used in some kind of listener in order to compare with currently last visible position, am I right ?

欢呼声, /马辛

推荐答案

只是为了推断在TomTo的回答,你如何可能会做一些这方面的思想(理论,我没有试过在实践中):

Just to extrapolate on TomTo's answer, some thoughts on how you might do this (theoretical, I haven't tried this in practice):

ListView lv = (ListView)findViewById(R.id.list_view);
lv.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, 
        int visibleItemCount, int totalItemCount) {
        //Check if the last view is visible
        if (++firstVisibleItem + visibleItemCount > totalItemCount) {
            //load more content
        }
    }
});

由于 firstVisibleItem 是返回一个索引(从0开始),我加1得到实际的项目数量。然后,如果是,加的可见项目的数目,大于的总项数,则在理论上,最后视图应该是可见的。不应该是一个问题,但请记住,这不叫,直到滚动完成。

Since firstVisibleItem is returns an index (0-based), I increment by one to get the actual item number. Then, if that, plus the number of visible items, is greater than the total number of items, then in theory the last view should be visible. Shouldn't be a problem, but keep in mind this isn't called till the scrolling is finished.

这篇关于onScrolling监听器绑定到Android的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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