Android Gridview 仅更新 ImageAdapter 外部的一个元素
我有一个 gridview,使用元素的图像适配器, 它的实现方式如下例所示: http://developer.android.com/resources/tutorials/views/hello -gridview.html
此外,我为 getView 方法中的每个项目添加了一个单击侦听器,该侦听器使用处理程序将单击的位置发送到主类(在 ImageAdapter 之外)。
现在我只想更新相关的 imageView,但是:
我不知道如何在 ImageAdapter 类之外获取 imageView(与处理程序一起发送?它不可序列化 - 在 ImageAdapter 和 getter 中创建一个缓冲区?)< /p>
我不确定使用哪种方法改变图像。
目前我每次都会更新整个网格:
((ImageAdapter)gridview.getAdapter()).setImages(imageIds);
gridview.invalidate();
ImageAdapter:
public void setImages(int[] images) {
mImages = images;
notifyDataSetChanged();
}
提前致谢
I have a gridview using an image adapter for the elements,
it's implemented like in this example:
http://developer.android.com/resources/tutorials/views/hello-gridview.html
Additionally I added a click listener for each item in the getView method, which sends the clicked position to the main class (outside of ImageAdapter) using a handler.
Now I want to update only the concerned imageView, but:
I don't know how to get the imageView outside of the ImageAdapter class (send with the handler? It's not serializable - create a buffer in ImageAdapter and getter?)
I'm not sure which method to use to change the image.
Currently I'm updating the whole grid each time:
((ImageAdapter)gridview.getAdapter()).setImages(imageIds);
gridview.invalidate();
ImageAdapter:
public void setImages(int[] images) {
mImages = images;
notifyDataSetChanged();
}
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你现在做的事情多少是正确的。
作为优化,您可以使用 ArrayList作为图像,并在适配器类中创建一个方法,以使用 ArrayList.set(int index, E 修改给定索引的值)元素)。然后调用
notifyDataSetChanged()
方法应该“理论上”仅更新更改后的图像视图:)What you are currently doing is somewhat correct.
As an optimization, you could use an
ArrayList<int>
as the images and create a method in your adapter class to modify the value of a given index usingArrayList.set(int index, E element)
. Then callingnotifyDataSetChanged()
method should "theoretically" update the changed image view only :)