调用 Baseadapter.getview() 方法时 ListView 变慢
我一直在调试我的应用程序,我看到当我滚动列表视图时,调用类 BaseAdapter 的 getView() 方法来生成新视图
public View getView(int position, View convertView, ViewGroup parent) {
Article article = this.articles.get(position);
return new MainView(this.context, articulo.getTitle() , articles.getDescription(),articles.getImgUrl());) }
,当我滚动 listActivity 以查看新项目时,会再次调用此方法来创建以下内容列表视图项目,由于列表项目有图像,ListActivity 变得很慢,有没有办法一次性创建所有项目视图,而不是在滚动 listActivity 时创建 ListItems
I have been debugging my application and i saw that when i was scrolling the listview the method getView() of the class BaseAdapter is called to generate new views
public View getView(int position, View convertView, ViewGroup parent) {
Article article = this.articles.get(position);
return new MainView(this.context, articulo.getTitle() , articles.getDescription(),articles.getImgUrl());) }
when i scroll the listActivity to see the new items this method is invoked again to create the below list view items, as a consequence that the list items have images the ListActivity get slow, is there any way to create all the items view once, and not create ListItems when we are scrolling the listActivity
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ListViews 针对性能进行了高度优化,您应该在 ListAdapter 中使用 ViewHolder 来缓存 ListItems。
检查 http://developer. android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
规则是,首先设置您的自定义视图,将所有内容打包到支架内并固定此支架到视图上,第二次使用 android 视图简单地提取持有者信息(非常快)。
ListViews are highly optimized for performance, you should use ViewHolder inside your ListAdapter to cache the ListItems.
check http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
the rule is, first set up your customview, pack everything inside your holder and pin this holder onto the view, the second time the view is used android simple extract the holder information (really fast).
由于创建的对象数量较多,速度可能会减慢。为了提高性能,您应该重用您的行。请参阅此处的
getView
实现:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List4.html 和 http://developer.android.com /resources/samples/ApiDemos/src/com/example/android/apis/view/List14.htmlIt's probably slowing down because of the number of objects that are created. For performance you should reuse your rows. See the
getView
implementation here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List4.html and http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html您不应在每次调用 getView 时创建新视图。传入的convertView 允许重用现有的View。在您的情况下,这将是 MainView 的一个实例。所以你可以这样做:
此外,你可以使用 Michele 建议的 ViewHolder 模式。这将允许您在设置标题等时避免 findViewById 查找。 这是 ViewHolder 的一个很好的解释。
You should not create a new View on each call to getView. The convertView that is being passed in allows use to reuse an existing View. In your case this will be an instance of MainView. So you can do something like this:
In addition, you could use the ViewHolder pattern suggested by Michele. This will allow you to avoid the findViewById lookups when setting title etc. Here is a great explanation of ViewHolder.