-1

我想知道 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);

谢谢!

4

1 回答 1

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>ListCollections有自己的方法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)方法。

例子

我有所有像你一样的整数数组。我已经在路上实施了。

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: 信息/家庭(2627): 2130837504
06-19 12:49:53.489: 信息/家庭(2627): 2130837505
06-19 12:49:53.489: 信息/家庭(2627): 2130837508
06-19 12:49:53.489: 信息/家庭(2627): 2130837507
06-19 12:49:53.489: 信息/家庭(2627): 2130837506
06-19 12:49:53.489: 信息/家庭(2627) : 2130837509
06-19 12:49:53.489: 信息/家庭(2627): 2130837510
06-19 12:49:53.489: 信息/家庭(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: 信息/家庭(2627): 2130837511
06-19 12:49:53.489: 信息/家庭(2627): 2130837504
06-19 12:49:53.489: 信息/家庭(2627): 2130837506
06-19 12:49:53.489:信息/家庭(2627):2130837508
06-19 12:49:53.489:信息/家庭(2627):2130837510
06-19 12:49:53.489:信息/家庭(2627) : 2130837505
06-19 12:49:53.489: 信息/家庭(2627): 2130837507
06-19 12:49:53.489: 信息/家庭(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;
}
于 2013-06-19T06:07:10.323 回答