如何使碎片加载更快?

6
我当前正在开发一个包含专辑列表的碎片。 其结构非常简单:
水平滑块包含LinearLayout(水平)。为了添加一个包含歌曲列表的专辑,我创建了一个单独的视图,其中包括封面图片和一个ListView。ListView也是自定义的,其中项是带有3个TextView的LinearLayout。然后我将其填充,将列表添加到水平滑块中。
问题出现在我有超过2个专辑列表时。打开碎片需要一些时间。
另一件事是当我尝试滚动(水平)时,有时会停顿一会儿,这会导致糟糕的用户体验。
我的意思是我已经看到类似的视图它们可以快速运行而且没有延迟。是否有可能进行优化?或者是否可以使碎片立即打开,然后列表稍后加载(一种懒加载方式)?
ListView项目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#ccffffff"
    android:padding="10dp" >

        <TextView
            android:id="@+id/album_list_item_number"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:text=""
            android:gravity="center|center_vertical"
            android:layout_gravity="center|center_vertical"
            android:textColor="#333333"
            android:textAppearance="?android:attr/textAppearanceMedium" />





        <TextView
            android:id="@+id/album_list_item_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_weight="1"
            android:gravity="left|center_vertical"
            android:layout_gravity="left|center_vertical"
            android:textColor="#333333"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/album_list_item_time"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text=""
            android:gravity="center|center_vertical"
            android:layout_gravity="center|center_vertical"
            android:textColor="#333333"
            android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

填充列表并向水平滑块添加视图:
    View albumView = inflater.inflate(R.layout.artist_album_col, container, false);
    //setting cover
    ImageView albumCover = (ImageView) albumView.findViewById(R.id.artistAlbumCover);
    albumCover.setImageDrawable(getResources().getDrawable(R.drawable.albumcover)); //cover

    ListView albumList = (ListView) albumView.findViewById(R.id.artistSongList);


    // create the grid item mapping
    String[] from = new String[] {"num", "title", "time"};
    int[] to = new int[] { R.id.album_list_item_number, R.id.album_list_item_title, R.id.album_list_item_time};

    // prepare the list of all records
    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    for(int i = 0; i < 24; i++){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("num", "" + i);
        map.put("title", "title title title title title title title title title " + i);
        map.put("time", "time " + i);
        fillMaps.add(map);
    }

    // fill in the grid_item layout
    SimpleAdapter adapter = new SimpleAdapter(mContext, fillMaps, R.layout.album_list_item, from, to);
    albumList.setAdapter(adapter);

    albumContainer.addView(albumView);

你能提供足够的代码,以便 Stack Overflow 社区可以审查并提出适当的建议吗?这样我们就能看到你是如何创建你的片段和适配器的。 - petey
1
使用Traceview来找出你的时间花在了哪里。 - CommonsWare
1个回答

0

您可以使用 异步任务 来执行耗时任务,并将它们从 UI 线程中移除。这将使片段快速加载并让列表“懒惰地”填充。

使用以下方式调用:

new LoadingTask().execute(""); 

你可以将类似这样的代码添加到你的片段类中(警告!未经测试):

private class LoadingTask extends AsyncTask<Void, Void, Void> {
   SimpleAdapter adapter;

   protected void doInBackground(Void... values) {

       // do your work in background thread 
       // create the grid item mapping                       
       String[] from = new String[] {"num", "title", "time"};                       
       int[] to = new int[] { R.id.album_list_item_number, R.id.album_list_item_title, R.id.album_list_item_time};

       // prepare the list of all records                         
       List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();                         
       for(int i = 0; i < 24; i++){                             
       HashMap<String, String> map = new HashMap<String, String>();                             
       map.put("num", "" + i);                             
       map.put("title", "title title title title title title title title title " + i);                             
       map.put("time", "time " + i);                             
       fillMaps.add(map);                         
     }                                              
     // fill in the grid_item layout                         
     adapter = new SimpleAdapter(mContext, fillMaps, R.layout.album_list_item, from, to); 
       return;
   }

   protected void onPostExecute(Void value) {

      // back in UI thread after task is done   
      ListView albumList = (ListView) getActivity().findViewById(R.id.artistSongList);     
      albumList.setAdapter(adapter);
   }
}

另一个例子在这里


1
我描述的延迟似乎与显示数据有关,而不是生成视图。因此,即使使用AsynTask,在onPostExecute发生时也会停止一秒钟。 - Arturs Vancans

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接