有没有一种方法,以prevent从滚动到它的顶部位置的列表视图当它的适配器的数据被改变? [英] Is there a way to prevent the listview from scrolling to its top position when its adapter's data is changed?

查看:121
本文介绍了有没有一种方法,以prevent从滚动到它的顶部位置的列表视图当它的适配器的数据被改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点麻烦$ P $的时候改变它的适配器的数据pserving列表视图的滚动位置。

I'm having a bit of trouble preserving the scroll position of a list view when changing it's adapter's data.

什么我目前做的是创建一个自定义ArrayAdapter(与重写getView方法)在的onCreate A ListFragment,然后将其分配给它的列表:

What I'm currently doing is to create a custom ArrayAdapter (with an overridden getView method) in the onCreate of a ListFragment, and then assign it to its list:

mListAdapter = new CustomListAdapter(getActivity());
mListAdapter.setNotifyOnChange(false);
setListAdapter(mListAdapter);

然后,当我收到新的数据加载器定期获取的一切,我这样做是在其 onLoadFinished 回调:

mListAdapter.clear();
mListAdapter.addAll(data.items);
mListAdapter.notifyDataSetChanged();

现在的问题是,调用明确()重置ListView的滚动位置。删除该调用preserves的位置,但它显然树叶在列表中的旧项目。

The problem is, calling clear() resets the listview's scroll position. Removing that call preserves the position, but it obviously leaves the old items in the list.

什么是正确的方式做到这一点?

What is the proper way to do this?

推荐答案

正如你指出你自己,调用清除()导致位置重置顶端。

As you pointed out yourself, the call to 'clear()' causes the position to be reset to the top.

与滚动位置等摆弄是一个黑客位来得到这个工作。

Fiddling with scroll-position, etc. is a bit of a hack to get this working.

如果从ArrayAdapter您CustomListAdapter子类,这可能是这个问题:

If your CustomListAdapter subclasses from ArrayAdapter, this could be the issue:

调用清除(),称之为notifyDataSetChanged()。您可以prevent这样的:

The call to clear(), calls 'notifyDataSetChanged()'. You can prevent this:

mListAdapter.setNotifyOnChange(false); // Prevents 'clear()' from clearing/resetting the listview
mListAdapter.clear();
mListAdapter.addAll(data.items);
// note that a call to notifyDataSetChanged() implicitly sets the setNotifyOnChange back to 'true'!
// That's why the call 'setNotifyOnChange(false) should be called first every time (see call before 'clear()').
mListAdapter.notifyDataSetChanged(); 

我还没试过此我自己,但尝试:)

I haven't tried this myself, but try it :)

这篇关于有没有一种方法,以prevent从滚动到它的顶部位置的列表视图当它的适配器的数据被改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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