我難怪有反正隨機圖像中的GridView隨機圖像中的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);
謝謝你!
是不是你的問題解決? –
不,還是解決不了。 – user1731690
好的。是否有可能用'ArrayList'替換Integer []?兩者具有相同的功能。如果你想做的話,我有其他解決方案。通過使用數組,您可以通過get(position)方法獲取任何值。 –