是否存在具有视图回收功能的图库小部件的替代品?
Android 上的默认 Gallery 小部件不会回收视图 - 每次调用新位置的视图时,小部件总是调用适配器的 getView
方法,并将 convertView
设置为 null。
当您向后和向前滚动时,最终会创建大量视图,而图库存储这些视图的回收器组件似乎无法足够快地回收它们,从而导致 OOM 情况。
您可以使用一些大图像作为图库项目轻松测试这一点,但最终仅使用 TextView 就会导致这种情况。在适配器的 getView 方法中放入带有计数器的日志语句,还可以查看创建了多少个新视图。
是否存在行为类似于 Gallery 但也实现视图回收的第三方小部件?
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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我使用了 http://code.google.com/p 的补丁/android/issues/detail?id=3376#c19
I used patch from http://code.google.com/p/android/issues/detail?id=3376#c19
参加聚会已经太晚了,但我已经修改了 EcoGallery 来做更多事情(并避免一些崩溃)。
我将其命名为 TimelineGallery,它与 Gallery 相同,但是它可以平滑滚动,并且在异步加载图像时不会做奇怪的事情。
为了演示它,该示例使用 Picasso 和 PullToRefresh。
原始代码、版权等都属于谷歌,因此责怪他们制造了如此蹩脚的小部件。
最后说明:我不建议使用画廊,它很旧,有缺陷,很糟糕,而且可能永远不会得到维护。问题不在于修复它的错误,问题在于画廊的整个架构是错误的,因此,在不引入更多黑客的情况下修复它是不可能的。
谷歌意识到了这一点并弃用了它。使用 ViewPager 或 HorizontalScrollList 并处理每个的限制。
如果您仍然想继续使用这个“图库”,请随意,它可以工作,但它可能会使您的应用程序崩溃并使您感到沮丧。
Super late to the party, but I've modified EcoGallery to do a few more things (and avoid some crashes).
I've called it TimelineGallery and it's the same crap as the Gallery, but it can do smooth scroll and doesn't do weird stuff when images are loaded asynchronously.
To demonstrate it, the sample uses Picasso and PullToRefresh.
The original code, copyright and such belongs to Google so blame them for making such a crappy widget.
Final note: I do not recommend using the gallery, it's old, buggy, hacky and will probably never be maintained. The problem is not about fixing its bugs, the problem is that the whole architecture of the Gallery is wrong and as such, fixing it is not possible without introducing more hacks.
Google realized this and deprecated it. Use a ViewPager or a HorizontalScrollList and deal with the limitations of each.
If you still want to go ahead and use this "gallery", feel free, it works, but it may crash your app and may frustrate you.
针对 OutOfMemory 问题的另一个更快的解决方法是尝试/捕获解码图像的代码,如果抛出 OutOfMemory 异常,则尝试再次使用较小的分辨率对其进行解码..
像这样:
当然现在可以,你会在不同的时间看到同一张图片的不同分辨率 - 但至少你的图库不会再崩溃,并且你总是显示你能看到的最高分辨率。
Another quicker WorkAround for the OutOfMemory Issues, is to try/catch the code where you decode the image and if the OutOfMemory-exception is thrown, you try to decode it with smaller Resolution again..
something like this:
Ofcourse it is now possible, that you will see different resolutions of the same picture at different times - But at least your Gallery will not crash anymore and you allways show the highest resolution you can.
最后,我的解决方案是按照 @CommonsWare 的建议修改 Gallery 源代码。这还需要复制以下文件:
AdapterView
AbsSpinner
但这些非常简单。
之后我修改了代码以执行以下操作:
通过这些修改,我的适配器中的
newView
方法中的计数器达到了... 7.此处是代码(于 2013/08/07 置于公共领域 http://en.wikipedia.org/wiki /WTFPL)
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
but these are pretty simple.
After that I modified code to do the following:
With these modifications my counter in my
newView
method in my adapter reached... 7.Here is the code (Placed in the public domain 2013/08/07 under http://en.wikipedia.org/wiki/WTFPL)
实际上还有一种替代方案,尽管我还没有亲自测试过:
https://github.com/falnatsheh/EcoGallery< /a>
Actually there is an alternative, though I haven't personally tested it:
https://github.com/falnatsheh/EcoGallery