0

我想在LinearLayout不阻塞 UI 的情况下向我的视图添加视图。

@Override
protected void onPostExecute(RequestMySellingList result)
{

   for (MySellingData data : result.data)
   {
         LinearLayout rowSelling = (LinearLayout) inflater.inflate(R.layout.row_selling_item, null);

         ImageView iv_sellingItemImage = (ImageView) rowSelling.findViewById(R.id.iv_sellingItemImage);

         iv_sellingItemImage.setImageBitmap(data.bitmap);

         // Add rowSelling to the main list holder
         ll_sellingList.addView(rowSelling);
  }
}

注意: ll_sellingListLinearLayout包含条目的

我无法使用onProgressUpdate(),因为我收到了很长的 json 响应,我必须使用onPostExecute()获取完整 json 请求的方法。

问题是如果请求很长 - addView 会阻止 UI

4

2 回答 2

0

它会阻塞 UI,因为inflater.inflate()是一个繁重的操作。而且,findViewById也不是便宜的手术。你在 for 循环中多次调用它们。因此,最好像这样将它们移出 for 循环。试试看它是否在不阻塞 UI 的情况下运行得更快。

@Override
protected void onPostExecute(RequestMySellingList result)
{

     LinearLayout rowSelling = (LinearLayout) inflater.inflate(R.layout.row_selling_item, null);

     ImageView iv_sellingItemImage = (ImageView) rowSelling.findViewById(R.id.iv_sellingItemImage);

     for (MySellingData data : result.data)
     {
          iv_sellingItemImage.setImageBitmap(data.bitmap);

          // Add rowSelling to the main list holder
          ll_sellingList.addView(rowSelling);
     }
}
于 2013-11-03T20:32:51.653 回答
0

I was able to do this when working with accordion style layout where each collapsing item hold also a subset of list as that kind of layout is not working well with RecyclerView Viewholder pattern. I used Concurrent as a replacement for Asynctask then on doInBackground all Glide fetch for images and addView() call was wrapped using new Handler(Looper.getMainLooper()).post(() -> {//Your Code}); since Glide and adding view requires it to be run on UI thread. The adding of each layout will be seen one by one in the screen but the good thing is no more frame skip from Choreographer.

于 2021-01-15T17:53:30.820 回答