保留在ListView的位置notifyDataSetChang

保留在ListView的位置notifyDataSetChang

本文介绍了在调用后保留在ListView的位置notifyDataSetChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OnScrollListener动态地添加项目到ListView当用户滚动至底部。后我将数据添加到适配器和呼叫notifyDataSetChanged虽然,在ListView追溯到到顶部。理想情况下,我想保留在ListView的位置。如何,我应该去这样做?

I'm using an OnScrollListener to dynamically add items to a ListView when the user scrolls to the bottom. After I add the data to the adapter and call notifyDataSetChanged though, the ListView goes back up to the top. Ideally, I would like to retain the position in the ListView. Any thoughts on how I should go about doing this?

推荐答案

难道这是你想要的吗?

// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();

// notify dataset changed or re-assign adapter here

// restore the position of listview
mList.setSelectionFromTop(index, top);

这篇关于在调用后保留在ListView的位置notifyDataSetChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:49