是否存在具有查看回收功能的图库小部件的替代品? [英] Does a replacement for Gallery widget with View recycling exist?

查看:12
本文介绍了是否存在具有查看回收功能的图库小部件的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 上的默认图库小部件不会回收视图 - 每次调用新位置的视图时,小部件总是调用带有 convertView 设置的适配器的 getView 方法为空.

The default Gallery widget on Android does not recycle views - everytime the view for a new position is called the widget always calls the getView method of the adapter with convertView set to null.

当您向后和向前滚动时,最终会创建大量视图,图库存储它们的回收器组件似乎没有足够快地回收它们,从而导致 OOM 情况.

As you scroll backwards and forwards this ends up in lots of views being created, which the recycler component that the Gallery stores them in does not seem to recycle them quickly enough leading to an OOM situation.

您可以使用一些大型图像作为您的图库项目轻松测试这一点,但最终只有 TextView 会导致它.在适配器的 getView 方法中放置一个带有计数器的日志语句,以查看创建了多少新视图.

You can test this easily with a few large-ish images as your gallery items, but just a TextView will cause it in the end. Put a log statement with a counter in the getView method of your adapter also to see how many new views are created.

是否存在行为类似于图库但也实现视图回收的第三方小部件?

Does a third-party widget which behaves like a Gallery but that also implements view recycling exist?

推荐答案

最后,我的解决方案是按照@CommonsWare 的建议修改 Gallery 源代码.这也需要复制以下文件:

My solution was, in the end, going with @CommonsWare's suggestion to modify the Gallery source code. This is also required copying the following files:

  • AdapterView
  • AbsSpinner

但这些都很简单.

之后我修改了代码以执行以下操作:

After that I modified code to do the following:

RecycleBin (AbsSpinner)

  • 将对象一个接一个地放入回收器中,而不是按照定位
  • 从回收器底部检索对象,无论请求职位
  • 现有实现假设适配器中的每个不同位置产生了独特的看法.仅当您的图库包含只有一种类型的项目,如果不是,您需要根据项目类型添加某种键以及所需的那种类型的数量

图库

  • 使用反射 (ugh) 修改 ViewGroup 的私有 mGroupFlags 变量以允许子重新排序 - 我还设置了一个布尔值,指示字段访问是否成功我在使用组件之前进行了测试.
  • 删除了对 mRecycler.clear()
  • 的所有调用
  • 数量画廊必须展示的项目随着它滚动和现有的实施将清除当 (a) setSelection 为 recycler称为 (b) 发生运动滚动
  • Used reflection (ugh) to modify the private mGroupFlags variable of ViewGroup to allow child re-ordering - I also set a boolean value indicating whether the field access succeeded which I test before using the component.
  • Removed all calls to mRecycler.clear()
  • The number of items the gallery has to display changes as it scrolls and the existing implementation would clear the recycler when (a) setSelection was called (b) a motion scroll occurred

通过这些修改,我的适配器中 newView 方法中的计数器达到了... 7.

With these modifications my counter in my newView method in my adapter reached... 7.

这是代码(于 2013/08/07 置于公共域 http://en.wikipedia.org/wiki/WTFPL)

Here is the code (Placed in the public domain 2013/08/07 under http://en.wikipedia.org/wiki/WTFPL)

这篇关于是否存在具有查看回收功能的图库小部件的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆