簡體   English   中英

invalidate()不會立即更改自定義視圖的內容

[英]invalidate() does not changes content of custom view immediately

我有一個包含自定義視圖的xml布局:

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:orientation="vertical"
        android:layout_weight="50" 
    >
        <org.example.sudoku.PuzzleView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/puzzlebackground"
            android:id="@+id/puzzleId"/>

    </LinearLayout>

我有一個活動,將這種布局設置為其內容,並調用一個對話框以供用戶輸入,該對話框又調用了PuzzleView中的一個函數,最終改變了PuzzleView的內容。 問題在於,關閉對話框后,更改尚未重新繪制。 相反,它會在用戶的下一個其他輸入上重繪。 以下是一些可以幫助您的代碼段:

Game.java:

// ...
public void showKeypadOrError(int x, int y)
{
    int tiles[] = getUsedTiles(x,y);
    if (tiles.length == 9)
    {
        Toast toast = Toast.makeText(this, R.string.no_moves_label, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }else
    {
        Log.d(TAG, "showKeypad: used=" + toPuzzleString(tiles));
        Dialog v = new Keypad(this, this.puzzleView, x, y);
        v.show();
    }
}
public void setTile(int x, int y, int value) {
    puzzle[y * 9 + x] = value;
}

Keypad.java:

private void setListeners()
{
    for (int i = 0; i < keys.length; i++)
    {
        final int t = i + 1;
        keys[i].setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) 
            {
                returnResult(t);
            }
        });
    }
}

private void returnResult(int tile)
{   
    puzzleView.setSelectedTile(selX, selY, tile);
    puzzleView.invalidate();
    dismiss();
}

最后,PuzzleView.java

public void setSelectedTile(int X, int Y, int tile) {
    game.setTile(X, Y, tile);
    //this.invalidate();// may change hints
}

我發現了一個看起來與我的問題相似的問題,但我不知道解決方案是什么:

從對話框返回時如何使invalidate()?

非常感謝您的幫助! 編輯:添加更多代碼-希望此幫助:PuzzleView.java

// Handle input in touch mode
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN) {
        return super.onTouchEvent(event);
    }

    // Allow player to choose the tile that defined by game
    // But only allow the user to modify the tiles that are blank
    select((int) (event.getX() / width),
            (int) (event.getY() / height));

    int[] predefined = new int[81];
    // Get the tile that are not blank (predefined by game)
    predefined = game.getPredefinedTileFromPuzzle();
    // Check if the selected tile is whether predefined or not
    if (predefined[selY * 9 + selX] == 1) {
        return true;
    }
    //Call the Dialog input from Game.java
    game.showKeypadOrError(selX, selY);
    Log.i(TAG, "Right before terminate onTouchEvent()");
    return true;
}

取消對話框后,您應該使之無效。

實現DialogInterface.OnDismissListener接口,並在OnDismiss回調中使其無效。

    Dialog v = new Keypad(this, this.puzzleView, x, y);

    v.setOnDismissListener(new OnDismissListener() {
    @Override
        public void onDismiss(final DialogInterface arg0) {
            puzzleView.postInvalidate ();
        }
    });

     v.show();

只是做puzzleView.requestLayout();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM