且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使片段加载速度更快?

更新时间:2023-02-06 22:22:11

您可以使用的异步任务来执行耗时的任务,并把他们关在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://***.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.