当前位置:首页 > 服务端 > 如何让你的listview更流畅

如何让你的listview更流畅

在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 });

 

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

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

 
 
 
 
 
 

作者:__木头鱼__
来源链接:https://www.cnblogs.com/elefish/archive/2013/01/30/2883291.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/125581.html

标签: Unnecessary
分享给朋友:

“如何让你的listview更流畅” 的相关文章

[C#]richtextbox实现行号2022年05月17日 20:27:54
[C#][控件]文本类控件2022年05月17日 20:45:24
计算机组成原理考试复习2022年05月18日 15:56:51
linux yum命令详解2022年05月19日 19:54:29
常用日志框架介绍2022年05月19日 20:04:06
多线程编程(1)2022年05月20日 21:26:51
Python 查找算法2022年05月20日 21:28:36