2012-09-11 47 views
5

我目前正在開發一個包含相冊列表的片段。 結構非常簡單:如何使碎片加載更快?

水平滑塊包含LinearLayour(水平)。爲了添加一個包含歌曲列表的專輯,我創建了一個包含封面圖片和列表視圖的獨立視圖。列表視圖也是自定義的,其中項目是3個textview的線性佈局。然後我膨脹它,填充列表,並添加到水平滑塊。

如果我有兩個以上的專輯列表,則會出現該問題。它需要一些時間來打開一個片段。

另一件事是當我嘗試滾動(水平),有時它會停下來片刻,這會造成不好的用戶體驗。

我想說的是,我看到了類似的看法,他們的工作很快,沒有滯後。以某種方式優化它可能嗎?或者有可能使片段立即打開,然後列表加載(類型爲lazyload)。

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); 
+0

你能提供足夠的代碼來看看你是如何創建你的片段和適配器的,以便SO社區能夠檢查它並提出適當的建議嗎? – petey

+0

使用Traceview來確定你的時間花在哪裏。 – CommonsWare

回答

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

+1

我描述的滯後似乎與顯示數據和不生成視圖有關。所以即使使用AsynTask,當onPostExecute發生時,它也會停止一秒。 –