2012-09-11 21 views
5

मैं वर्तमान में एक टुकड़ा विकसित कर रहा हूं जिसमें एल्बम की सूचियां हैं। संरचना बहुत सरल है:टुकड़े कैसे तेजी से लोड करते हैं?

क्षैतिज स्लाइडर में LinearLayour (क्षैतिज) शामिल है। गीत सूची के साथ एक एल्बम जोड़ने के लिए मैंने एक अलग दृश्य बनाया है जिसमें कवर छवि और एक सूचीदृश्य शामिल है। सूचीदृश्य कस्टम भी है जहां आइटम 3 टेक्स्टव्यू के साथ लाइनरलेआउट हैं। फिर मैं इसे फुलाता हूं, सूची तैयार करता हूं, और क्षैतिज स्लाइडर में जोड़ता हूं।

समस्या तब होती है जब मेरे पास 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); 
+0

क्या आप यह देखने के लिए पर्याप्त कोड प्रदान कर सकते हैं कि आप अपने टुकड़े और एडेप्टर कैसे बनाते हैं ताकि SO समुदाय इसकी समीक्षा कर सके और उचित सुझाव दे सके? – petey

+0

पता लगाने के लिए ट्रेसव्यू का उपयोग करें कि आपका समय कहां खर्च किया जा रहा है। – CommonsWare

उत्तर

0

आप Async Task उपयोग कर सकते हैं समय लेने वाली कार्य करने के लिए और उन्हें यूआई धागा दूर ले जाने । यह टुकड़े को जल्दी से लोड करने की अनुमति देगा और सूची "आलसी" को पॉप्युलेट करने की अनुमति देगी।

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: साथ

कॉल:

new LoadingTask().execute(""); 

और आप अपने टुकड़ा कक्षा में इस तरह एक वर्ग छड़ी कर सकते हैं (चेतावनी परीक्षण नहीं!)।

+1

मैंने जो अंतराल वर्णित किया है वह डेटा प्रदर्शित करने और दृश्य उत्पन्न करने से संबंधित प्रतीत होता है। तो AsynTask का उपयोग करने पर, यह एक दूसरे के लिए रुक जाएगा जब पोस्टस्टेक्स्यूट होता है। –

संबंधित मुद्दे