【问题标题】:How to make fragments load faster?如何让片段加载更快?
【发布时间】:2012-09-11 12:58:53
【问题描述】:

我目前正在开发一个包含专辑列表的片段。 结构很简单:

水平滑块包含 LinearLayour(水平)。为了添加带有歌曲列表的专辑,我创建了一个单独的视图,其中包含封面图像和列表视图。列表视图也是自定义的,其中项目是带有 3 个文本视图的线性布局。然后我给它充气,填充列表,然后添加到水平滑块。

如果我的专辑列表超过 2 个,则会出现此问题。打开一个片段需要一些时间。

另一件事是当我尝试(水平)滚动时,有时它会停止一小会儿,这会导致糟糕的用户体验。

我想说的是,我看到了类似的观点,而且他们的工作很快,没有滞后。是否有可能以某种方式对其进行优化?或者是否有可能让片段立即打开,然后加载列表(一种延迟加载)。

列表查看项目:

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

【问题讨论】:

  • 您能否提供足够的代码来了解您是如何创建片段和适配器的,以便 SO 社区能够对其进行审查并提出适当的建议?
  • 使用 Traceview 找出您的时间都花在了哪里。

标签: android android-fragments


【解决方案1】:

您可以使用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

【讨论】:

  • 我描述的滞后似乎与显示数据而不是生成视图有关。所以即使使用 AsynTask,它也会在 onPostExecute 发生时停止一秒钟。
猜你喜欢
  • 2018-09-07
  • 2021-06-08
  • 2012-01-08
  • 2014-12-14
  • 2020-03-21
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多