在列表视图重绘的单行视图、列表

2023-09-12 00:24:54 作者:初心

是否有可能重新绘制在的ListView 单行?我有一个的ListView 与那些的LinearLayout s行。我听一个preference变化,有时我需要改变只是一个查看的LinearLayout A单行。有没有一种方法,以使其重绘该行不调用 listview.notifyDatasetChanged()

Is it possible to redraw a single row in a ListView? I have a ListView with rows that are LinearLayouts. I listen to a preference change and sometimes I need to change just one View inside the LinearLayout of a single row. Is there a way to make it redraw that row without calling listview.notifyDatasetChanged()?

我已经打过电话view.invalidate()上的观点(在的LinearLayout ),但它不重绘行。

I've tried calling view.invalidate() on the view (inside the LinearLayout) but it doesn't redraw the row.

推荐答案

由于罗曼盖伊解释了一会儿回谷歌I / O会议上期间,最有效的办法只能更新列表视图中的一个视图是类似如下(这个更新整个查看数据):

As Romain Guy explained a while back during the Google I/O session, the most efficient way to only update one view in a list view is something like the following (this one update the whole View data):

ListView list = getListView();
int start = list.getFirstVisiblePosition();
for(int i=start, j=list.getLastVisiblePosition();i<=j;i++)
    if(target==list.getItemAtPosition(i)){
        View view = list.getChildAt(i-start);
        list.getAdapter().getView(i, view, list);
        break;
    }

假设目标是适配器的一个项目。

Assuming target is one item of the adapter.

这code检索的ListView ,然后浏览当前显示的看法,比较目标项目您正在寻求与每个显示视图中的项目,如果你的目标是在那些,得到封闭视图和执行适配器 getView()在该视图刷新显示。

This code retrieve the ListView, then browse the currently shown views, compare the target item you are looking for with each displayed view items, and if your target is among those, get the enclosing view and execute the adapter getView() on that view to refresh the display.

作为一个侧面说明无效()作品并不像一些人预计,并不会像刷新视图getView()确实 notifyDataSetChanged()将重建整个名单,并最终调用 getview()为每个显示项目和 invalidateViews()也会影响了一堆。

As a side note invalidate() doesn't work like some people expect and will not refresh the view like getView() does, notifyDataSetChanged() will rebuild the whole list and end up calling getview() for every displayed items and invalidateViews() will also affect a bunch.

最后一件事,人们还可以得到额外的性能,如果他只需要改变一排视图的孩子,而不是像 getView 整行一样。在这种情况下,下面的code可以替换 list.getAdapter()getView(一,查看,清单); (例如更改的TextView 文本):

One last thing, one can also get extra performance if he only needs to change a child of a row view and not the whole row like getView does. In that case, the following code can replace list.getAdapter().getView(i, view, list); (example to change a TextView text):

((TextView)view.findViewById(R.id.myid)).setText("some new text");

在我们信任code。