gpt4 book ai didi

android - 如何使 fragment 加载更快?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:11 24 4
gpt4 key购买 nike

我目前正在开发一个包含专辑列表的 fragment 。结构非常简单:

水平 slider 包含 LinearLayour(水平)。为了添加带有歌曲列表的专辑,我创建了一个单独的 View ,其中包含封面图像和 ListView 。 ListView 也是自定义的,其中项目是具有 3 个 TextView 的线性布局。然后我膨胀它,填充列表,并添加到水平 slider 。

如果我有 2 个以上的专辑列表,就会出现此问题。打开 fragment 需要一些时间。

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

我想说的是,我看到过类似的观点,而且它们工作迅速,没有滞后。有没有可能以某种方式优化它?或者是否有可能直接打开 fragment ,然后加载列表(有点像延迟加载)。

ListView 项目:

<?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 添加到水平 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);

最佳答案

您可以使用 Async Task执行耗时的任务并将它们从 UI 线程中移除。这将允许 fragment 快速加载并“延迟”填充列表。

调用:

new LoadingTask().execute(""); 

你可以在 fragment 类中添加这样的类(警告!未测试):

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 .

关于android - 如何使 fragment 加载更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12370286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com