图库 ImageAdapter convertView 始终为空 [英] Gallery ImageAdapter convertView is always null

查看:18
本文介绍了图库 ImageAdapter convertView 始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 ImageAdapter 的 Gallery 来使用 ImageViews 加载它,从而从我的资源中提取图像.我的问题是,在我的适配器中传递给 getView() 方法的 convertView 始终为空.这意味着每次调用 getView() 时都会创建一个新的 ImageView.这会导致可怕的性能,因为 GC 不断运行以清除所有这些已创建且不再使用的 ImageView.

I am using a Gallery with an ImageAdapter to load it with ImageViews that pull images out of my resources. My problem is that the convertView that gets passed to the getView() method in my adapter is always null. This means that a new ImageView is created each and every time getView() is called. This leads to horrible preformance because the GC is constantly running to wipe away all of these created and no longer used ImageView's.

这显然是一个已知错误:图库的视图缓存已损坏;从不转换视图..

This is apparently a known bug: Gallery's view cache is broken; never converts views..

我的两个首选解决方案是: 1. 在适配器本身中处理视图缓存并处理正确重用它们所需的所有逻辑.或 2. 包含我自己的 Gallery 小部件副本并尝试修复它,以便正确返回回收视图.

My two preferred solutions are either 1. handle a cache of views in the adapter itself and take care of all the logic required to re-use them properly. or 2. include a my own copy of the Gallery widget and try to fix it so it properly returns recycled views.

我已经开始实施选项一,但我很快意识到我并不完全知道如何制作该操作背后的所有逻辑.我开始认为选项二可能更容易.

I've started implementing option one but am quickly realizing I don't exactly know how to make all of the logic behind that operation. I am begining to think that option two might be easier.

我在这里找到了图库小部件的代码:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/widget/Gallery.java

I've found the code for the Gallery widget here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/widget/Gallery.java

虽然不是很懂,但是可以看出来是在调用

I don't fully understand it, but I can see that it is calling

child = mAdapter.getView(position, null, this);

在第 745 行.我(在黑暗中拍摄)猜测这是问题的根源.

on line 745. My (shot in the dark) guess that this is the root of the problem.

有没有人遇到过这个错误.或者任何人都可以指出我正确的方向来弄清楚回收商的情况是如何工作的,以便我可以调整这个小部件以使其正常工作?甚至建议一些我可能忽略的替代选项.

Does anyone have experience with this bug. Or can anyone point me in the right direction for figuring out how the recycler situation works so that I can tweak this widget to work correctly? Or even suggest some alternate option that I may be overlooking.

我发现的最佳解决方案是一个名为 EcoGallery 的实现.我唯一能找到参考它的地方 在线.要使其正常工作,您必须将每个块从那里放入项目中的正确位置.

The best solution that I ever found was an implementation called EcoGallery. The only place I can find reference to it online anymore is here. To get it working you have to put each chunk from there in the correct place within your project.

推荐答案

没有特别处理过这个错误的经验,但我之前使用 3rd 方视图寻呼机(在支持库之前)完成了自定义缓存.

No experience with that bug particularly, but I have done custom caching before with a 3rd party view pager (before the support lib).

老实说,这真的不是太难.就我而言,我知道屏幕上最多只有一个项目.但我也希望预加载左右的项目(这些是从网络上提取数据的网络视图).所以我有一个简单的 3 个视图数组.

It's really not too difficult honestly. In my case, I knew there would be, at most, one item on the screen. But I also wanted the item to the left and right to be preloaded (these were webviews pulling data off the net). So I have a simple array of 3 views.

[V1, V2, V3]

现在您唯一需要做的就是将适配器中的位置与缓存中的位置相关联.有多种方法可以解决这个问题.当我第一次使用它时,我只是将我当前的视图设置为 V2,并在翻转寻呼机时旋转了数组的项目.所以翻转到下一个视图会改变我的数组

Now the only thing you need to do is correlate a position in your adapter to a position in your cache. There's a variety of ways you could tackle this. When I first spiked it out, I just made whatever my current view was to be V2 and the rotated the items of the array around when flipping the pager. So flipping to the next view would alter my array

[V2, V3, V1]

很容易跟上.或者你可以只做数学计算并计算一个位置到缓存的相对位置.

Was simple to keep up with. Or you could just do the math and calculate a position to a relative position of the cache.

另一种方法是创建后进先出队列.通过将视图推入队列来回收视图,当您需要视图时,只需从中弹出一个即可.

Another approach is to create a last in, first out queue. Recycle views by pushing them into the queue, and when you need a view, just pop one from it.

这篇关于图库 ImageAdapter convertView 始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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