Android:Gridview正在缓存过时的项目,如何确保在重新加载页面时为每个显示项目调用getview?
上下文:
我有一个包含 6 个选项卡的主要活动,每个选项卡包含一个包含 30 多个图像的网格视图,并且随时显示大约 8 个图像(每个网格视图都是可伸缩的)。
有了这么多图像,我实现了一个简单的缓存系统,即只缓存其中最新的 25 个,如果需要显示新图像并且不在缓存中,我将从 /data/date 目录中解码该图像并覆盖缓存中最旧的条目。在覆盖最旧的条目之前,我将对其调用 Bitmap.recycle() 。
问题:
在某些时候,显示超过 25 个图像后,旧图像将开始被回收。如果我现在导航回其图像不再位于缓存中的选项卡之一,GetView 方法将小心地再次加载图片。问题是,除了 Gridview 中的第一个项目将调用 GetView 之外,图像的其余部分将永远不会被调用,并且 Gridview 将尝试显示已被回收的缓存图像。
有什么建议吗?在这种情况下,要么强制获取视图,要么以智能方式缓存图像?
非常
感谢
Context:
I have a main activity holding 6 tabs, each tabs holds a gridview with 30+ images, and around 8 images a shown at any moment (each gridview is scollable).
With these many images I implemented a simple caching system, that is to only cache the latest 25 of them, if new image is needed to display and is not in the cache, I will decode that image from /data/date directory and overwrite the oldest entry in the cache. And before overwriting the oldest entry, I will call Bitmap.recycle() on that.
Problem:
At some point, after displying more than 25 images, old images will start to be recycled. if I were to now navigate back to one of the tab where their Image is no longer in the cache, the GetView method will take care to load the picture again. The Issue is other than the first Item in the Gridview will be calling GetView, rest of the image will never get call, and the Gridview will attempt to display a cached image where it has been recycled already.
Any Suggestion? either to force get view or a smart way to cache image in this scenario?
Much Appreciated
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过创建自己的 ImageView 来扩展 Android ImageView 来解决这个问题。然后重写 onDraw(Canvas canvas) 方法并添加逻辑以在缓存消失时重新加载缓存。
看起来很哈基......但按预期工作。如果有人可以教我,我真的希望我能做得更好。非常感谢!!
I kind of solved this by creating my own ImageView extending the Android ImageView. Then Overrided the onDraw(Canvas canvas) method and added logic to reload cache if cache is gone.
looks Hacky... but worked as wanted. really wish i cna do better than that if there is someone who can teach me. Thanks a ton!!