如何使片段加载速度更快? [英] How to make fragments load faster?
问题描述
我目前正在开发,其中包含专辑列表的片段。
其结构是pretty简单:
水平滑块包含LinearLayour(水平)。为了与歌曲列表中添加一张专辑我创建了包括封面图片和列表视图的单独的视图。 ListView控件是自定义的,以及在那里项与3 textviews LinearLayout中。然后我它充气,填充列表,并添加到水平滑块。
如果我有2个以上的专辑列表时,会出现问题。这需要一些时间来打开的片段。
另一件事是,当我尝试滚动(水平),有时它会停止片刻,这使得不好的用户体验。
我所想说的是,我已经看到了类似的观点,他们快速,无滞后工作。是否有可能以某种方式进行优化呢?或者说,该片段打开直线距离,并有可能再列表负载之后(一种lazyload的)。
ListView项:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向
机器人:背景=#ccffffff
机器人:填充=10dp> <的TextView
机器人:ID =@ + ID / album_list_item_number
机器人:layout_width =30dp
机器人:layout_height =WRAP_CONTENT
机器人:文字=
机器人:比重=中心| center_vertical
机器人:layout_gravity =中心| center_vertical
机器人:文字颜色=#333333
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/> <的TextView
机器人:ID =@ + ID / album_list_item_title
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=
机器人:layout_weight =1
机器人:重力=左| center_vertical
机器人:layout_gravity =左| center_vertical
机器人:文字颜色=#333333
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/> <的TextView
机器人:ID =@ + ID / album_list_item_time
机器人:layout_width =90dp
机器人:layout_height =WRAP_CONTENT
机器人:文字=
机器人:比重=中心| center_vertical
机器人:layout_gravity =中心| center_vertical
机器人:文字颜色=#333333
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>< / LinearLayout中>
填充列表并添加视图到水平滑块:
查看albumView = inflater.inflate(R.layout.artist_album_col,集装箱,FALSE);
//设定封面
ImageView的albumCover =(ImageView的)albumView.findViewById(R.id.artistAlbumCover);
albumCover.setImageDrawable(getResources()getDrawable(R.drawable.albumcover)); //盖 ListView控件albumList =(ListView控件)albumView.findViewById(R.id.artistSongList);
//创建网格项映射
的String [] =由新的String [] {NUM,标题,时间};
INT []为= INT新[] {R.id.album_list_item_number,R.id.album_list_item_title,R.id.album_list_item_time}; // prepare的所有记录列表
清单<&HashMap的LT;字符串,字符串>> fillMaps =新的ArrayList<&HashMap的LT;字符串,字符串>>();
的for(int i = 0; I< 24;我++){
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
map.put(民,+ I);
map.put(称号,标题标题标题标题标题标题标题标题标题为+ I);
map.put(时间,时间+ I);
fillMaps.add(地图);
} //填写grid_item布局
SimpleAdapter适配器=新SimpleAdapter(mContext,fillMaps,R.layout.album_list_item,从,到);
albumList.setAdapter(适配器); albumContainer.addView(albumView);
您可以使用的异步任务来执行耗时的任务,并把他们关在UI线程。这将允许快速加载片段和列表来填充懒洋洋地。
与电话:
新LoadingTask()执行()。
和你能坚持这样一类的片段类(警告没有测试!):
私有类LoadingTask扩展的AsyncTask<太虚,太虚,太虚> {
SimpleAdapter适配器; 保护无效doInBackground(虚空......值){ //做你在后台线程工作
//创建网格项映射
的String [] =由新的String [] {NUM,标题,时间};
INT []为= INT新[] {R.id.album_list_item_number,R.id.album_list_item_title,R.id.album_list_item_time}; // prepare的所有记录列表
清单<&HashMap的LT;字符串,字符串>> fillMaps =新的ArrayList<&HashMap的LT;字符串,字符串>>();
的for(int i = 0; I< 24;我++){
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
map.put(民,+ I);
map.put(称号,标题标题标题标题标题标题标题标题标题为+ I);
map.put(时间,时间+ I);
fillMaps.add(地图);
}
//填写grid_item布局
适配器=新SimpleAdapter(mContext,fillMaps,R.layout.album_list_item,从,到);
返回;
} 保护无效onPostExecute(空值){ //早在UI线程后任务完成
ListView控件albumList =(ListView控件)getActivity()findViewById(R.id.artistSongList)。
albumList.setAdapter(适配器);
}
}
另一个例子 href=\"http://stackoverflow.com/questions/9671546/asynctask-android-example\">。
I am currently developing a fragment which contains lists of albums. The structure is pretty simple:
Horizontal slider contains LinearLayour (horizontal). In order to add an album with songs list I have created a separate view which consists of cover image and a listview. The listview is custom as well where items are linearlayout with 3 textviews. Then I inflate it, populate list, and add to the Horizontal slider.
The issue occurs if I have more than 2 album lists. It takes some time to open a fragment.
Another thing is when I try to scroll (horizontally), sometimes it stops for a short moment, which makes bad user experience.
What I am trying to say is that I have seen similar views and they work quickly and with no lags. Is it possible somehow to optimize it? Or is it possible so that the fragment opens straight away and then the lists load afterwards (kind of lazyload).
LISTVIEW ITEM:
<?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>
POPULATING THE LIST AND ADDING VIEW TO THE HORIZONTAL SLIDER:
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);
You can use Async Task to perform the time-consuming tasks and take them off the UI thread. This will allow the fragment to load quickly and the list to populate "lazily".
Call with:
new LoadingTask().execute("");
And you can stick a class like this in your fragment class (warning! not tested):
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);
}
}
Another example here.
这篇关于如何使片段加载速度更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!