GridLayout的程序逻辑错误

时间:2018-01-28 11:18:37

标签: java android collections

帮助了解问题所在。我写了这段代码,它描述了这些行为:

    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();

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

not shuffle

1 个答案:

答案 0 :(得分:1)

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