安卓:检测是否一个​​ListView有滚动条(制定新的数据后) [英] Android: Detect if a ListView has the scrollbar (after setting new data)

查看:91
本文介绍了安卓:检测是否一个​​ListView有滚动条(制定新的数据后)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接到一个ListView的ArrayAdapter。

I have an ArrayAdapter linked to a ListView.

mListView.setAdapter(mArrayAdapter);

每当我ArrayList的数据恢复到一个ArrayAdapter:

Whenever I reset the ArrayList data to the ArrayAdapter:

mArrayAdapter.clear();
mArrayAdapter.addAll(mArrayList);
mArrayAdapter.notifyDataSetChanged()

在ListView得到正确更新

the ListView gets correctly updated

但是,如果仅仅上述三个行之后,我把我的自定义方法 mListView.hasScrollbar()来检测列表视图是否具有滚动条或者不是,我得到一个空 lastVisibleItem

however, if just after the above three lines, I call my custom method mListView.hasScrollbar() to detect whether the listview has a scrollbar or not, I get a null lastVisibleItem:

public boolean hasScrollbar() {
    View lastVisibleItem = (View) getChildAt(getChildCount() - 1);
    if (lastVisibleItem.getBottom()>=getHeight()) {
        return true;
    }
    return false;
}

这是否意味着ListView控件仍然是令人耳目一新?

does it mean that the listview is still refreshing?

我的主要问题是:结果
我怎么能测试,如果列表视图有新的数据重置适配器后滚动条?

My main question is:
how can I test if the listview has the scrollbar after resetting the adapter with new data?

感谢您的帮助!

推荐答案

使用getLastVisiblePosition / getFirstVisiblePosition是检测阉羊您在列表视图内滚动与否(aslong因为你把它比作getCount将(一个有效的方法)并执行数学OFC)。
你有因为你已经猜到的问题是,你正试图签不同步。
为了同步您的查询时,该适配器已经充满了你的列表数据和更新的变化,需要发出一个POST请求到列表中,这将堆叠该项呈请适配器的消息队列。

Using getLastVisiblePosition / getFirstVisiblePosition is a valid method of detecting wether you have scrolling or not within the list view (aslong as you compare it to getCount() and do your math ofc). The problem you have as you already guess is that you are attempting to check out of sync. In order to sync your query when the adapter already filled your List Data and updated changes, you need to issue a post request to the list, which will stack that petition to the message queue of the adapter.

yourAdapter.notifyDataSetChanged();
yourAdapter.getListView().post(new Runnable() { 
    @Override public void run() { 
        //your code here        
    } 
});

确认打电话,经过当然notifySetDataChanged()。因为你想要列表前检查更新。

Make sure to call that after notifySetDataChanged() of course. Because you want the list to update before the check.

这篇关于安卓:检测是否一个​​ListView有滚动条(制定新的数据后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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