【问题标题】:notifyDataSetChanged() without freezing mainthreadnotifyDataSetChanged() 不冻结主线程
【发布时间】:2014-07-30 15:41:18
【问题描述】:

我有列表视图和自定义适配器,它们使用我自己的对象来绘制列表项。

从另一个角度来看,我有一个实时收集信息的服务,我的活动每 0.1 秒调用一次服务信息,然后通过调用 myCustomAdapter.notifyDataSetChanged() 方法重绘列表视图。

这对我来说很糟糕,因为对象非常大,我的 UI 线程冻结了一段时间可能不到 0.01 秒,但用户体验仍然很差。

我正在更新的资源是由画布中的自定义绘图类绘制的一个圆圈。你们中有人知道如何处理这个问题吗?有没有办法在不停止我的 UI 线程的情况下更新数据和重绘列表视图?

【问题讨论】:

  • 你需要展示一些代码来获得帮助。
  • 你的getView方法优化了吗?您可以重用视图(作为 convertView 传递)
  • 没有优化,有什么好的优化教程吗?
  • 我回复了一个例子
  • @BorisPawlowski 供将来参考,请努力正确输入您的问题并正确格式化。

标签: android multithreading listview


【解决方案1】:

您应该尝试重用您的 convertView,如下所示:

public View getView (int position, View convertView, ViewGroup parent){
    //inflate the view if it is null
    if( convertView == null ){
        convertView = inflater.inflate(R.layout.my_list_item, parent, false);
    }
    //make the changes on your  convertView that are changed from row to row,
    //such as a text in a TextView
    return convertView;
}

【讨论】:

  • 我用了这个,它解决了我的问题。非常感谢,这是我想了一天多的问题。非常感谢!!!
猜你喜欢
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
相关资源
最近更新 更多