6

現在、アルバムのリストを含むフラグメントを開発しています。構造は非常に単純です。

水平スライダーには、LinearLayour (水平) が含まれています。曲リスト付きのアルバムを追加するために、カバー画像とリストビューで構成される別のビューを作成しました。リストビューはカスタムであり、項目は 3 つのテキストビューを備えた線形レイアウトです。次に、それを膨らませてリストに入力し、水平スライダーに追加します。

この問題は、2 つ以上のアルバム リストがある場合に発生します。フラグメントを開くには時間がかかります。

もう 1 つの問題は、(水平方向に) スクロールしようとすると、一時的に停止することがあり、ユーザー エクスペリエンスが低下することです。

私が言おうとしているのは、同様のビューを見たことがあり、それらは迅速かつ遅延なく機能するということです. どういうわけかそれを最適化することは可能ですか?または、フラグメントがすぐに開き、その後リストがロードされるようにすることは可能ですか (一種の遅延ロード)。

リストビュー項目:

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

1 に答える 1

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

ここに別の例があります。

于 2012-09-11T14:24:13.817 に答える