如何使片段加载更快?

时间:2012-09-11 12:58:54

标签: android android-fragments

我目前正在开发一个包含相册列表的片段。 结构非常简单:

水平滑块包含LinearLayour(水平)。为了添加带有歌曲列表的专辑,我创建了一个单独的视图,其中包括封面图像和列表视图。列表视图也是自定义的,其中项目是linearlayout,包含3个textviews。然后我给它充气,填充列表,然后添加到水平滑块。

如果我有超过2个专辑列表,则会出现此问题。打开片段需要一些时间。

另一件事是当我尝试滚动(水平)时,有时会停止片刻,这会导致糟糕的用户体验。

我想说的是,我看到了类似的观点,他们的工作很快,没有滞后。有可能以某种方式优化它吗?或者是否有可能让片段立即打开,然后列表加载(延迟加载)。

LISTVIEW ITEM:

<?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);

1 个答案:

答案 0 :(得分:0)

您可以使用Async Task执行耗时的任务并将其从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);
   }
}

另一个例子here