【问题标题】:Loading listView.setAdapter in main thread takes too much time在主线程中加载 listView.setAdapter 需要太多时间
【发布时间】:2016-02-09 15:08:46
【问题描述】:

所以基本上我可能有 50 条带有图像和文本等的记录。每个列表项中没有要加载的数据太多,但是当我调用 setAdapter 时,它至少需要 8 秒才能加载前 3 个项目对最终用户可见。我添加了日志,发现适配器中的 getView() 被调用了两次。有什么办法可以减少这个时间吗?

在任何人问之前,我不能使用 AsyncTask,因为我需要在主线程中调用 setAdapter 并且问题仍然存在。

使用 RecycleView 会提高效率吗?我知道 RecycleView 是 listView 的下一个版本,但在设置适配器时我看不到任何特别的区别。

感谢任何帮助!谢谢

更新:这是我的适配器代码:

        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder view;

        if (convertView == null) {
            view = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.fragment_test, null);

            view.inActiveView = convertView.findViewById(R.id.front);
            view.inActiveRewardTxt = (TextView) convertView.findViewById(R.id.status_lock_reward);
            view.inActiveTitleTxt = (TextView) convertView.findViewById(R.id.status_lock_title);

            view.completedView = convertView.findViewById(R.id.offerTitlePanel);

            view.activeView = convertView.findViewById(R.id.back);
            view.offerNumberTxt = (TextView) convertView.findViewById(R.id.offerNumberTxt);

            view.walletView = convertView.findViewById(R.id.offerWalletPanel);
            view.walletGiftView = (ImageView) convertView.findViewById(R.id.offerGiftImg);
            view.rewardTxt = (TextView) convertView.findViewById(R.id.offerWalletRibbonTxt);
            view.offerTitleTxt = (TextView) convertView.findViewById(R.id.offerTitleTxt);
            view.offerDescTxt = (TextView) convertView.findViewById(R.id.pagerDescTaskTxt);
            view.offerActionTxt = (TextView) convertView.findViewById(R.id.offerActionTxt);
            view.offerTimerTxt = (TextView) convertView.findViewById(R.id.offerTimerTxt);
            view.offerStatusBtn = (TextView) convertView.findViewById(R.id.offerStatusBtn);
            view.offerReportBtn = (TextView) convertView.findViewById(R.id.offerReportBtn);

            view.dataProgressBar = (ProgressBar) convertView.findViewById(R.id.data_loader);
            view.dataLoadedView = convertView.findViewById(R.id.loaded_view);
            view.calendarView = (CalendarView) convertView.findViewById(R.id.calendar_view);

            view.dataView = convertView.findViewById(R.id.dataView);
            view.dataInfoTxt = (TextView) convertView.findViewById(R.id.dataInfoTxt);
            view.dataEarnedTxt = (TextView) convertView.findViewById(R.id.dataEarnedTxt);
            view.dataProgress = (ProgressBar) convertView.findViewById(R.id.dataProgress);

            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }

  //display all the values here

return convertView;

【问题讨论】:

  • 你在使用视图持有者模式和延迟加载吗?
  • 我在我的自定义适配器类中使用 ViewHolder 模式。我使用 imageLoader 库将图像加载到列表视图
  • 发布您的适配器代码并阅读stackoverflow.com/questions/11945563/…。最好切换到 recyclerview
  • 谢谢,我会研究 recycleView 看看它是否有任何显着差异
  • 您的列表中有很多视图,看看您是否可以减少它。看看您是否可以使用自定义视图和视图组,因为在某些情况下它可以提供良好的性能

标签: android multithreading performance listview


【解决方案1】:

仅从您提供的代码来看,加载 3 个视图似乎不需要 8 秒。您是否有可能在 UI 线程上进行任何其他繁重的处理?

【讨论】:

    【解决方案2】:

    所以我使用 recyclerView 来解决这个问题。感谢您的所有帮助!

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      相关资源
      最近更新 更多