Android:是否可以仅刷新列表视图中的一项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2953381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Is it possible to refresh just one item in a listview?
提问by wei
I'm wondering if it is possible to rerender just one element in a listview? I assume by calling notifyDatasetChanged()
is gonna rerender the whole list?
我想知道是否可以在列表视图中只重新渲染一个元素?我假设通过调用notifyDatasetChanged()
会重新呈现整个列表?
Thanks,
谢谢,
采纳答案by CaseyB
You can, but it's a bit convoluted. You would have to get the index of the first visible item in the list and then use that do decide how how far down in the list of visual items the item is that needs updated, then grab its view and update it there.
你可以,但它有点复杂。您必须获取列表中第一个可见项目的索引,然后使用它来决定该项目在可视项目列表中需要更新多远,然后获取其视图并在那里更新。
It's much easier to just call notifyDatasetChanged()
.
只需调用notifyDatasetChanged()
.
回答by Driss Bounouar
you can't render (refresh) a single row, but instead you can get the requested view and make chages on it directly by calling yourListView.getChildAt(int VisiblePosition);
where the visiblePostion is the position in the ListView minus yourListView.getFirstVisiblePosition()
您无法渲染(刷新)单行,而是可以通过调用yourListView.getChildAt(int VisiblePosition);
其中的 visiblePostion 是 ListView 中的位置减去来获取请求的视图并直接对其进行更改yourListView.getFirstVisiblePosition()
Like this :
像这样 :
View v = listViewItems.getChildAt(position -
listViewItems.getFirstVisiblePosition());
v.setBackgroundColor(Color.GREEN);
I hope this helps...
我希望这有帮助...
回答by Bobs
Also you can use this:
你也可以使用这个:
myListView.invalidateViews();
回答by greeshma
dataAdapter.remove(dataAdapter.getItem(clickedpos));
dataAdapter.insert(t.getText().toString(), clickedpos);
回答by Ali
This is how I did it:
我是这样做的:
Your items (rows) must have unique ids so you can update them later. Set the tag of every view when the list is getting the view from adapter. (You can also use key tag if the default tag is used somewhere else)
您的项目(行)必须具有唯一的 ID,以便您可以稍后更新它们。当列表从适配器获取视图时,设置每个视图的标签。(如果在其他地方使用默认标签,您也可以使用键标签)
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
view.setTag(getItemId(position));
return view;
}
For the update check every element of list, if a view with given id is there it's visible and update must be performed on it.
对于更新检查列表的每个元素,如果具有给定 id 的视图在那里,它是可见的,并且必须对其执行更新。
private void update(long id)
{
int c = list.getChildCount();
for (int i = 0; i < c; i++)
{
View view = list.getChildAt(i);
if ((Long)view.getTag() == id)
{
// update view
}
}
}
It's actually easier than other methods and better when you dealing with ids not positions! Also you must consider scenario when your view get invisible and visible again.
当您处理 ids 而不是位置时,它实际上比其他方法更容易和更好!此外,您必须考虑当您的视图变得不可见并再次可见时的场景。
回答by radhoo
You need to keep track of your adapter (or custom adapter if you are set on fancy features). When you change the data for an item, simply change the fields you are interested in , in your adapter. Then call notifyDatasetChanged , and the changes will be reflected in your listview.
您需要跟踪您的适配器(或自定义适配器,如果您设置了奇特的功能)。当您更改项目的数据时,只需在您的适配器中更改您感兴趣的字段。然后调用 notifyDatasetChanged ,更改将反映在您的列表视图中。
Note that this approach works exactly the same for Gallery Views as well.
请注意,这种方法对于画廊视图也完全相同。