单词搜索游戏:如何通过拖动选择GridView中的多个项目?(机器人)

use*_*402 7 select android gridview cpu-word drag

这里是程序细节:所以我正在创建一个单词搜索游戏,就像有一堆字母排列在一个正方形中,你必须找到并选择垂直,水平或对角的单词.我在板上使用了一个字符串数组,并使用ArrayAdapter将这个字符串数组存储在gridview中,这是我主要活动布局的基本元素.

这里是问题:我如何制作它以便用户可以将手指拖过他们的选择,选择单词中的所有字母而不必多次将手指从屏幕上抬起来?并且我希望所选单词上的突出显示保留在屏幕上,我选择了一个单词,并且如果用户没有正确选择单词,当用户从屏幕上抬起手指时,我希望字母上的突出显示消失.

Ric*_*rdo 1

我迟到了这个问题,但最近遇到了类似的问题,我决定创建自己的自定义板视图库来解决这个问题。它为您可以做的事情增加了更多灵活性,并且实现更简单。

以下是一些显示其工作原理的代码片段。首先,很容易在自定义视图的方法上绘制板网格onDraw()

@Override
protected void onDraw(Canvas canvas) {
    for (int i = 0; i <= numRows; i++) {
        //For custom grid sizes, y can't be equal the board size or the line drawn won't show
        int y = Math.min(boardHeight - 1, i * tileSize);
        canvas.drawLine(0, y, boardWidth, y, boardPaint);
    }

    for (int i = 0; i <= numCols; i++) {
        //For custom grid sizes, x can't be equal the board size or the line drawn won't show
        int x = Math.min(boardWidth - 1, i * tileSize);
        canvas.drawLine(x, 0, x, boardHeight, boardPaint);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您有一个String[][]字符串数组,则假设每个图块的视图已放置在板上,则可以按以下方式设置字母:

public void setLetterBoard(String[][] letterBoard) {
    if (letterBoard.length != getNumRows()
            || letterBoard[0].length != getNumCols()) {
        setBoardSize(letterBoard.length, letterBoard[0].length);
    }

    int row, col;
    for (int i=0; i < getChildCount(); i++) {
        row = i / getNumCols();
        col = i % getNumCols();

        LetterTileView child = (LetterTileView) getChildAt(i);
        child.setLetter(letterBoard[row][col]);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要更复杂的工作来实际处理触摸并在棋盘图块上拖动以选择单词:

@Override
public boolean onTouchEvent(MotionEvent event) {
    float X = event.getX();
    float Y = event.getY();
    int row = (int) (Y / getTileSize());
    int col = (int) (X / getTileSize());

    View child = getChildAt(row, col);

    //Exit on invalid touches
    if (event.getActionMasked() != MotionEvent.ACTION_UP
            && (row >= getNumRows()
            || col >= getNumCols()
            || child == null)) {
        return true;
    }

    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            Tile currentTile = new Tile(row, col, child);
            if (currentSelectedWord == null) {
                currentSelectedWord = new SelectedWord(currentTile);
            } else if (!currentTile.equals(currentSelectedWord.lastTile)
                    && currentSelectedWord.isTileValid(currentTile)) {
                if (!currentSelectedWord.isTileAllowed(currentTile)) {
                    //Clear the status of the old selection
                    updateTiles(currentSelectedWord.selectedTiles, false, false);
                    //If the current tile is valid but not allowed for the current word selection,
                    //start a new selection that matches the tile
                    currentSelectedWord = new SelectedWord(currentSelectedWord.getInitialTile());
                }
                List<Tile> tiles = getTilesBetween(currentSelectedWord.lastTile, currentTile);
                if (tiles.size() > 0) {
                    currentSelectedWord.addTiles(tiles);
                }
            }
            updateTiles(currentSelectedWord.selectedTiles, true, false);
            break;
        case MotionEvent.ACTION_UP:
            if (currentSelectedWord != null) {
                boolean isValidSelection = (listener != null && listener.onWordSelected(currentSelectedWord.toBoardWord()));
                updateTiles(currentSelectedWord.selectedTiles, false, isValidSelection);
                if (isValidSelection) {
                    selectedWords.add(currentSelectedWord);
                }
                currentSelectedWord = null;
            }
            break;
        default:
            return false;
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

这些方法isTileValid()确保isTileAllowed()用户尝试选择的图块位于允许的方向并且之前未被选择过。最后,getTilesBetween()返回用户触摸的第一个图块和他/她当前正在触摸的图块之间的所有有效图块。

希望能帮助到你!