gpt4 book ai didi

java - GridLayout 的程序逻辑错误

转载 作者:行者123 更新时间:2023-12-02 11:41:54 26 4
gpt4 key购买 nike

帮助了解问题可能是什么。我编写了这段代码,它有操作的描述:

    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

最佳答案

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com