[英]Listview + BaseAdapter - how to notify about change in single item?
I have a list view fed by BaseAdapter. 我有一个由BaseAdapter提供的列表视图。
When something changes in data, I call BaseAdapter.notifyDataSetChanged. 当数据发生变化时,我调用BaseAdapter.notifyDataSetChanged。 All works fine. 一切正常。
Now I wonder, if I change some tiny detail in some list item, is there some optimized way to update view of single item if it's displayed on screen ? 现在我想知道, 如果我更改某些列表项中的一些细节,是否有一些优化的方法来更新单个项目的视图,如果它显示在屏幕上 ? I suppose that notifyDataSetChanged blindly rebuilds views of all visible items in list, which is not optimal if trivial change in item happens. 我想notifyDataSetChanged盲目地重建列表中所有可见项的视图,如果项中发生了微不足道的变化,这不是最佳的。
No, I guess it's not possible unless you implement your own ListView
class extension. 不,我想除非你实现自己的ListView
类扩展,否则它是不可能的。 Here there is the source code for setAdapter()
. 这里有setAdapter()
的源代码 。
You will see, the ListView
only registers itself as observer using mAdapter.registerDataSetObserver(mDataSetObserver);
您将看到, ListView
仅使用mAdapter.registerDataSetObserver(mDataSetObserver);
将自身注册为观察者mAdapter.registerDataSetObserver(mDataSetObserver);
And a DataSetObserver
provides no means to notify about a change at a certain position. 并且DataSetObserver
无法通知某个位置的更改。
However it might not be necessary to notify about updates of certain items, because as far as I know, a ListView
only renders and updates the items currently seen on the screen so, no optimization should be necessary here. 但是,可能没有必要通知某些项目的更新,因为据我所知, ListView
只呈现和更新当前在屏幕上看到的项目,因此这里不需要进行优化。
Yes, there is way. 是的,有方法。 Assuming I can identify list item, for example by assigning View.setTag to it to some value, I can iterate list items and rebind only one list item if desired, or even update only some sub-view of the item. 假设我可以识别列表项,例如通过将View.setTag分配给某个值,我可以迭代列表项并根据需要仅重新绑定一个列表项,或甚至只更新项的某个子视图。
It's simple and relatively cheap (linear search): 它简单而且相对便宜(线性搜索):
for(int i = list.getChildCount(); --i>=0; ){
View v = list.getChildAt(i);
Object id = v.getTag();
if(id==myId){
updateListItem(id, v);
break;
}
}
where myId is some ID of item I want to update, and updateListItem makes desired changes on the list item. 其中myId是我想要更新的项的ID,updateListItem对列表项进行了所需的更改。 Proved, and working fine and very efficiently. 事实证明,工作正常,效率很高。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.