2012-09-11 20 views
5

Actualmente estoy desarrollando un fragmento que contiene listas de álbumes. La estructura es bastante simple:¿Cómo hacer que los fragmentos se carguen más rápido?

El control deslizante horizontal contiene LinearLayour (horizontal). Para agregar un álbum con la lista de canciones, he creado una vista separada que consiste en una imagen de portada y una vista de lista. La vista de lista también es personalizada, donde los elementos son linearlayout con 3 textviews. Luego lo inflaré, completaré la lista y lo agregaré al control deslizante Horizontal.

El problema ocurre si tengo más de 2 listas de álbumes. Lleva un tiempo abrir un fragmento.

Otra cosa es cuando trato de desplazarme (horizontalmente), a veces se detiene por un momento, lo que hace que la experiencia del usuario sea mala.

Lo que trato de decir es que he visto vistas similares y que funcionan rápidamente y sin retrasos. ¿Es posible de alguna manera optimizarlo? ¿O es posible para que el fragmento se abra inmediatamente y luego las listas se carguen después (tipo de carga lenta).

Listview ARTÍCULO:

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

poblar la lista y SUMAR VISTA a la horizontal RESBALADOR:

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

¿Puedes proporcionar suficiente código para ver cómo creas tus fragmentos y adaptadores para que la comunidad de SO pueda revisarlo y hacer las sugerencias adecuadas? – petey

+0

Use Traceview para descubrir dónde se está gastando su tiempo. – CommonsWare

Respuesta

0

Puede utilizar Async Task para realizar las tareas que consumen mucho tiempo y llevarlos fuera el hilo de interfaz de usuario . Esto permitirá que el fragmento se cargue rápidamente y que la lista se llene "perezosamente".

llamada con:

new LoadingTask().execute(""); 

Y se puede pegar una clase como esta en su clase fragmento (advertencia no probado!):

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

Otro ejemplo here.

+1

El retraso que describí parece estar relacionado con la visualización de datos y la no generación de vistas. Entonces, incluso usando AsynTask, se detendrá por un segundo cuando onPostExecute ocurra. –

Cuestiones relacionadas