2012-09-11 3 views
5

현재 앨범 목록이 포함 된 조각을 개발 중입니다. 구조는 매우 간단합니다 :조각을 빨리로드하는 방법?

가로 슬라이더에 LinearLayour (가로)가 있습니다. 노래 목록이있는 앨범을 추가하려면 표지 이미지와 목록보기로 구성된 별도의보기를 만들었습니다. listview는 항목이 3 개의 텍스트 뷰가있는 선형 레이아웃 인 맞춤형입니다. 그런 다음 그것을 팽창시키고, 목록을 채우고, 수평 슬라이더에 추가합니다.

두 개 이상의 앨범 목록이있는 경우 문제가 발생합니다. 단편을 여는 데 약간의 시간이 걸립니다.

또 다른 것은 스크롤하려고 할 때 (가로 방향), 때로는 잠깐 멈추어 사용자 경험이 나빠지는 경우입니다.

내가 말하고자하는 것은 필자도 비슷한 견해를 보았고 신속하고 신속하게 일한다는 것입니다. 어떻게 든 그것을 최적화 할 수 있습니까? 또는 조각이 곧바로 열리면 목록이 나중에로드 될 수 있습니다 (일종의 lazyload).

목록보기 항목 :

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

목록을 채우고 가로 슬라이더 TO VIEW 추가 :

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

당신은 시간이 많이 걸리는 작업을 수행하고, UI 스레드를 벗고 Async Task을 사용할 수 있습니다 . 이렇게하면 조각이 빠르게로드되고 목록이 "느슨하게"채워집니다.

전화로 : 당신이 (! 경고 테스트되지 않음) 조각 클래스에서이 같은 클래스를 부착 할 수

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가 발생하면 잠시 멈 춥니 다. –

관련 문제