1

Is there anyway to customize to random image in GridView from this link

I am working on it and it give a very good result, but the image in gridview was not randomly. Can anyone guide me some techniq to customize this?

Thank you!

4

1 回答 1

0

我假设你正在做这样的事情:

Integer[] images = {
            R.drawable.1, R.drawable.2,
            R.drawable.3, ... ,R.drawable.n
    };

现在你有一个对你的drawables的引用数组。

List<Integer> imagesArrayList = new ArrayList<Integer>(Arrays.asList(images))

这会将您的数组转换为 ArrayList

Collections.shuffle(imagesArrayList);

来自文档:

Collections.shuffle :使用默认随机源随机排列指定列表。

然后在getView方法中(我假设您正在使用示例中的自定义适配器)

int[] tempArrayList = Ints.toArray(imagesArrayList);
imageView.setImageResource(tempArrayList[position]);

PS 没有编译,可能有错误

于 2013-06-26T11:40:15.793 回答