开发者

Android Gridview update only one Element from outside the ImageAdapter

开发者 https://www.devze.com 2023-04-03 11:42 出处:网络
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

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 ser开发者_StackOverflow社区ializable - 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


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 using ArrayList.set(int index, E element). Then calling notifyDataSetChanged() method should "theoretically" update the changed image view only :)

0

精彩评论

暂无评论...
验证码 换一张
取 消