Gridview setEnabled不适用于第一项

时间:2013-04-29 17:53:44

标签: android android-listview android-adapter

我今天有一个奇怪的问题。我正在尝试实现一个游戏,我有一些带有一些按钮的gridview。单击任何按钮时,将禁用它(使用setEnabled(false))。问题是,只有第一个按钮没有被禁用。虽然日志输出显示为其调用了setEnabled(false)

这是我的代码:

public class ButtonAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private GameScreen gameScreen;
    private Hangman hangman;
    private static final String[] buttonValues = 
        {
            "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U",
            "",  "V", "W", "X", "Y", "Z", "" ,
        };
    private Button[] holder = new Button[getCount()];

    public ButtonAdapter(GameScreen gameScreen) {
        inflater = (LayoutInflater) gameScreen
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.gameScreen = gameScreen;
    }

    public View getView(final int position, View convertView, ViewGroup arg2) {
        View view = convertView;
        if(view == null) {
            view = inflater.inflate(R.layout.game_button, null);

            final Button button = (Button) view.findViewById(R.id.button);
            button.setText(buttonValues[position]);
            holder[position] = button;
            view.setTag(holder[position]);
        }
        else
            holder[position] = (Button) view.getTag();

        holder[position].setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if(buttonValues[position].length() != 0) {
                    Hangman.GameState gameState = hangman.updateCurrentWord(
                            buttonValues[position].charAt(0));
                    holder[position].setEnabled(false);

                    gameScreen.updateView(gameState);
                    Log.d("Button", position + ":" + holder[position].getText());
                }
            }
        });

        return view;
    }

    public int getCount() {
        return buttonValues.length;
    }

    public Object getItem(int position) {
        return buttonValues[position];
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return holder[position].isEnabled();
    }
}

我也试过((Button) view.findViewById(R.id.button)).setOnClickListener(...);,但没有改变。

1 个答案:

答案 0 :(得分:4)

有时这些事情会发生,因为您正试图访问尚未膨胀的视图。试试这个:

    View view = convertView;

    if(view == null) 
    {
        view = inflater.inflate(R.layout.game_button, null);
        vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Override public void onGlobalLayout() 
        {
            final Button button = (Button) view.findViewById(R.id.button);
            button.setText(buttonValues[position]);
            holder[position] = button;
            view.setTag(holder[position]);

            ViewTreeObserver obs = view.getViewTreeObserver();
            obs.removeGlobalOnLayoutListener(this);
        }});
    ...

或者可能将ViewTreeObserver放在其他地方。这完全取决于代码首先进入if statement的哪一侧。