帮助您了解问题所在。我编写了这段代码,其中包含操作说明:

    int countPosition = 0;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            GridLayout.Spec buttonRowSpec = GridLayout.spec(i);
            GridLayout.Spec buttonSpecColumn = GridLayout.spec(j);
            TouchAction actionListener = new TouchAction(gridLayout, buttons, gameController);
            ImageButton button = ButtonsFactory.createImageButton(this, bitmaps[i][j], countPosition, paddingImgBtn, actionListener);
            countPosition++;
            if (countPosition < 12)
                buttons.add(button); // add each button to the collection
            // add each button in the GridLayout with the specified parameters
            gridLayout.addView(button, new GridLayout.LayoutParams(buttonRowSpec, buttonSpecColumn));
        }
    }

    gridLayout.removeAllViewsInLayout(); // remove the buttons from GridLayout
    Collections.shuffle(buttons); // mix the collection with the buttons
    Bitmap lastImageBitmap = ImageProcessor.resizeImage(getResources(), R.drawable.locked, bitmaps[0][0].getWidth(), bitmaps[0][0].getHeight(), true);
    lastButton.setImageBitmap(lastImageBitmap);
    buttons.add(lastButton); // add a button with a picture lock at the very end of the collection

    for (int i = 0; i < 12; i++) {
        ImageButton button = buttons.get(i); // get the buttons from the mixed collection
        gridLayout.addView(button); // add them to GridLayout
    }
    // do the validation that would be displayed correctly, but something tells me that this method is not for this!
    // in SWING, JPanel has a method like validate (), decided that it's the same here, but alas, apparently not
    gridLayout.invalidate();


输出图像不是逻辑所期望的,按钮应该已经混合:

java - GridLayout的程序逻辑错误-LMLPHP

最佳答案

创建按钮时,将在GridLayout中设置按钮的位置,按钮在列表中的顺序对布局没有影响。

您可以使用GridLayout.LayoutParams创建一个列表,并对其进行随机播放:

List<GridLayout.LayoutParams> params = new ArrayList<>();
for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
        GridLayout.Spec buttonRowSpec = GridLayout.spec(i);
        GridLayout.Spec buttonSpecColumn = GridLayout.spec(j);
        TouchAction actionListener = new TouchAction(gridLayout, buttons, gameController);
        ImageButton button = ButtonsFactory.createImageButton(this, bitmaps[i][j], countPosition, paddingImgBtn, actionListener);
        countPosition++;
        buttons.add(button); // add each button to the collection
        params.add(new GridLayout.LayoutParams(buttonRowSpec, buttonSpecColumn))
    }
}

Collections.shuffle(params); // mix the collection with the buttons

for (int i = 0, size = rows * cols; i < size; i++) {
    ImageButton button = buttons.get(i); // get the buttons from the mixed collection
    // add each button in the GridLayout with the specified parameters
    gridLayout.addView(button, params.get(i)); // add them to GridLayout
}
Bitmap lastImageBitmap = ImageProcessor.resizeImage(getResources(), R.drawable.locked, bitmaps[0][0].getWidth(), bitmaps[0][0].getHeight(), true);
lastButton.setImageBitmap(lastImageBitmap);
buttons.add(lastButton); // add a button with a picture lock at the very end of the collection

关于java - GridLayout的程序逻辑错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48485753/

10-11 19:59