本文介绍了如何检查我的 ListView 何时完成重绘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 ListView
.我更新了它的适配器,并调用 notifydatasetchanged()
.我想等到列表完成绘制,然后在列表上调用 getLastVisiblePosition() 以检查最后一项.
I have a ListView
. I updated its adapter, and call notifydatasetchanged()
. I want to wait until the list finishes drawing and then call getLastVisiblePosition() on the list to check the last item.
在 notifydatasetchanged()
之后立即调用 getLastVisiblePosition()
不起作用,因为列表尚未完成绘制.
Calling getLastVisiblePosition()
right after notifydatasetchanged()
doesn't work because the list hasnt finished drawing yet.
推荐答案
希望这可以帮助:
- 在列表视图上设置一个
addOnLayoutChangeListener
- 调用
.notifyDataSetChanged()
; - 这将在完成后触发
OnLayoutChangeListener
- 移除监听器
在更新时执行代码(
getLastVisiblePosition()
在您的情况下)
mListView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
mListView.removeOnLayoutChangeListener(this);
Log.e(TAG, "updated");
}
});
mAdapter.notifyDataSetChanged();
这篇关于如何检查我的 ListView 何时完成重绘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!