调用 Baseadapter.getview() 方法时 ListView 变慢

发布于 2024-12-04 08:47:03 字数 467 浏览 0 评论 0原文

我一直在调试我的应用程序,我看到当我滚动列表视图时,调用类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽袖吟 2024-12-11 08:47:03

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).

高跟鞋的旋律 2024-12-11 08:47:03

由于创建的对象数量较多,速度可能会减慢。为了提高性能,您应该重用您的行。请参阅此处的 getView 实现:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List4.htmlhttp://developer.android.com /resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

It'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

洋洋洒洒 2024-12-11 08:47:03

您不应在每次调用 getView 时创建新视图。传入的convertView 允许重用现有的View。在您的情况下,这将是 MainView 的一个实例。所以你可以这样做:

MainView mv;
if (convertView != null){
  mv = (MainView) convertView;
  ((TextView) mv.findViewById(R.id.title)).setText(articulo.getTitle());
  // similar for description and imgUrl
} else {
  mv = new MainView(...);
}
return mv;

此外,你可以使用 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:

MainView mv;
if (convertView != null){
  mv = (MainView) convertView;
  ((TextView) mv.findViewById(R.id.title)).setText(articulo.getTitle());
  // similar for description and imgUrl
} else {
  mv = new MainView(...);
}
return mv;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文