我目前正在开发一个包含专辑列表的片段。
结构相当简单:
水平滑块包含线性排列(水平)。为了添加带有歌曲列表的相册,我创建了一个单独的视图,该视图由封面图像和列表视图组成。ListView是自定义的,其中的项是用3个文本视图进行线性布局的。然后我充气,填充列表,并添加到水平滑块。
如果我有两个以上的相册列表,则会出现此问题。打开一个碎片需要一些时间。
另一件事是,当我尝试滚动(水平)时,有时它会短暂停止,这会造成糟糕的用户体验。
我想说的是,我看到过类似的观点,他们工作迅速,没有滞后。是否有可能以某种方式优化它?或者有没有可能让片段直接打开,然后列表随后加载(有点懒散)。
列表视图项:
<?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);
最佳答案
您可以使用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。