我很遗憾GridView中的随机图像无论如何
下面的代码对我来说效果很好但是在启动活动时GridView中没有随机图像。
我应该修改下面的代码以使其随机?
布局:
<GridView
android:id="@+id/fruit_gridview"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:smoothScrollbar="true" />
</RelativeLayout>
图像适配器:
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
public Integer[] mThumbIds = {
R.drawable.apple, R.drawable.apricot,
R.drawable.banana, R.drawable.bell,
R.drawable.blueberries, R.drawable.broccoli,
R.drawable.carrot, R.drawable.celery,
R.drawable.cherry, R.drawable.chili,
R.drawable.coconut, R.drawable.dragon_fruit,
R.drawable.durian, R.drawable.eggplant,
R.drawable.erdnuss, R.drawable.grape,
R.drawable.guava, R.drawable.kiwi,
R.drawable.lettuce, R.drawable.lychee,
R.drawable.mango, R.drawable.mangosteen,
R.drawable.passion, R.drawable.mengkudu,
R.drawable.mushroom, R.drawable.onion,
R.drawable.orange, R.drawable.pear,
R.drawable.pineapple, R.drawable.pomegranate,
R.drawable.potato, R.drawable.pumpkin,
R.drawable.radish, R.drawable.strawberry,
R.drawable.tomato, R.drawable.watermelon
};
GridView活动:
GridView gridview = (GridView) findViewById(R.id.fruit_gridview);
gridview.setAdapter(new ImageAdapter_Fruit_24(this));
final ImageAdapter_Fruit_24 imageAdapter = new ImageAdapter_Fruit_24(this);
谢谢!
答案 0 :(得分:2)
在显示或提供适配器之前,请尝试以下代码。
// Fisher–Yates shuffle
static void shuffleArray(Integer[] intArr) {
Random rnd = new Random();
for (int i = intArr.length - 1; i >= 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int temp = intArr[index];
intArr[index] = intArr[i];
intArr[i] = temp;
}
}
您也可以将其转换为ArraryList<Integer>
或List
。 Collections
有自己的方法shuffle
。
List list = Arrays.asList( solutionArray );
Collections.shuffle(list);
修改强>
将Integer[]
转换为ArrayList
。
ArrayList<Integer> arrList = new ArrayList<Integer>();
for(int i = 1 ; i < intArr.length ; i++)
{
arrList.add(intArr[i]);
}
Collections.shuffle(arrList);
要使用arrList
,请使用arrList.get(position)
方法。
示例强>
我拥有和你一样的所有Integer数组。我已经实施了。
Log.d("Home","Before Shuffle");
for(int i=0;i< mThumbIds.length;i++){
Log.i("Home",""+mThumbIds[i]);
}
输出将如下。 (不要与您的输出进行比较。资源ID可能不同)
06-19 12:49:53.489:INFO / Home(2627):2130837504
06-19 12:49:53.489:INFO / Home(2627):2130837505
06-19 12:49:53.489:INFO / Home(2627):2130837508
06-19 12:49:53.489:INFO / Home(2627):2130837507
06-19 12:49:53.489:INFO / Home(2627):2130837506
06-19 12:49:53.489:INFO / Home(2627):2130837509
06-19 12:49:53.489:INFO / Home(2627):2130837510
06-19 12:49:53.489:INFO / Home(2627):2130837511
现在,我将此Integer[]
转换为ArrayList<Integer>
。
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0;i< mThumbIds.length;i++){
list.add(mThumbIds[i]);
}
现在,随机播放列表并打印。
Collections.shuffle(list);
for(int i=0;i< list.size();i++){
Log.i("Home",""+list.get(i));
}
输出。 (每次运行代码时,输出都会有所不同。所以不要与之前的代码进行比较。)
06-19 12:49:53.489:INFO / Home(2627):2130837511
06-19 12:49:53.489:INFO / Home(2627):2130837504
06-19 12:49:53.489:INFO / Home(2627):2130837506
06-19 12:49:53.489:INFO / Home(2627):2130837508
06-19 12:49:53.489:INFO / Home(2627):2130837510
06-19 12:49:53.489:INFO / Home(2627):2130837505
06-19 12:49:53.489:INFO / Home(2627):2130837507
06-19 12:49:53.489:INFO / Home(2627):2130837509
您可以看到列表的顺序。它与以前不同。在您的代码中使用。
GridView gridview = (GridView) findViewById(R.id.fruit_gridview);
//do shuffle of list. make sure you have write above code before this
Collections.shuffle(list);
ImageAdapter_Fruit_24 imageAdapter = new ImageAdapter_Fruit_24(this);
gridview.setAdapter(imageAdapter);
在getView()
。
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
//here changes will be done
imageView.setImageResource(list.get(position));
return imageView;
}