鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > cms安装教程 > WordPress > >

如何让你的listview更流畅

来源:互联网 作者:佚名 时间:2015-10-18 05:57
在viewholder和asynctask的帮助下可以把listview做的 流畅 些(见android traing中的相关文章,如上一篇博文的末尾), 如何 更 流畅 ? 在国外的博客上找到一篇博文:https://sriramramani.wordpress.com/2012/07/22/recycling-listview-rows/ 方法是将已经进入re

在viewholder和asynctask的帮助下可以把listview做的流畅些(见android traing中的相关文章,如上一篇博文的末尾),如何流畅?

在国外的博客上找到一篇博文:https://sriramramani.wordpress.com/2012/07/22/recycling-listview-rows/

方法是将已经进入recycler的item中不需要的内容设为null, 让它被gc掉.这样可以省出很多memory.

原文如下, 单词障碍应该很少,就不费时间翻译了.感谢原作者.

=========================================================================

The ListViews in Android work on a combination of two things:

  1. Something that supplies the data – Adapter
  2. Something that supplies the view – getView() in the Adapter

Lets say there are 100 entries in a list and the screen size of the device can only show 15 of them at a time. Android does not create all 100 views, adding to a scrolling list, and show it to us. This causes unnecessary view inflations and hence slowing down the app. Instead, Android calls getView()only for the views that are shown — so only 15 in this case. When the user starts scrolling, the views that went out of the screen’s display are moved to a heap and new views are obtained through a call to getView(). When the user scrolls back to the initial set, Android tries to reuse the view it first created from the heap. This is what is passed as convertView to the getView()call. There are many blogs on perfecting the getView() using ViewHolder pattern.

When the Android moves the view to the heap, it gives us a chance to do anything with the view — like resetting the text in the view or removing images. Lets say the list we have is a list of images like in Instagram. Every time the user scrolls, more views are created and the older views are moved to heap. Even though the views are moved to heap, they still holds the huge ImageView’s drawable with them. This takes up so much memory.How can we avoid it? Lets make use of the chance that Android gives us.

 1 theListView.setRecyclerListener(new RecyclerListener() {
 2         @Override
 3         public void onMovedToScrapHeap(View view) {
 4             // Cast the view to the type of the view we inflated.
 5             MyCustomListRow row = (MyCustomListRow) view;
 6  
 7             // Get the view that holds the image and nullify the drawable.
 8             // GC will take care of cleaning up the memory used.
 9             row.getThatImageDrawableView().setImageDrawable(null);
10         }
11 });

 

====================================================

我自己没有实验过, 但应该可行.

 
 
 
 
 
 
网友评论
<