【问题标题】:Wrong program logic for GridLayoutGridLayout 的程序逻辑错误
【发布时间】:2018-01-28 11:18:36
【问题描述】:

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

    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 android collections


    【解决方案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
    

    【讨论】:

    • 非常感谢!我现在明白了问题所在。答案已接受
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    相关资源
    最近更新 更多