我想知道 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) findViewById(R.id.fruit_gridview);
gridview.setAdapter(new ImageAdapter_Fruit_24(this));
final ImageAdapter_Fruit_24 imageAdapter = new ImageAdapter_Fruit_24(this);
谢谢!