我正在构建一个使用GrirdView充满按钮的应用。我意识到在更改按钮颜色后,gridView适配器不会立即更新它。我尝试过多次更改代码,但它没有改变任何内容。 这是我的游戏"函数(这里我称之为drawTheMove,然后尝试使用线程添加中断):
if (isValid(p)) {
addChoice(p);
drawTheMove(btn);
if (!onePlayer) {
gameScreen.changeImageTurn(numOfClick);
turn++;//if there is only one player, keep the turn at "his side"
}
if (compLevel != 0) { //vs computer
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
drawTheMove:
public void drawTheMove(Button btn) {
btn.setText(Integer.toString(getNumOfClick()));
Player player = getLastPlayerPlay();
btn.setBackgroundResource(player.getImageId());
gameScreen.board.gridViewAdapter.notifyDataSetChanged();
gameScreen.board.boardGrid.invalidateViews();
}
我不确定它是否相关,但这是来自适配器的getView:
public View getView(final int position, View convertView, ViewGroup viewGroup) {
View row = convertView;
ViewHolder holder;
//checks if the gridview is new- to inflate only once.
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_item, viewGroup, false);
holder = new ViewHolder(row);
row.setTag(holder);
}
//else- using the old holder.
else {
//using the Tag to save the findViewById
holder = (ViewHolder) row.getTag();
}
holder.btnSqure = changeBtnSize(holder.btnSqure);
if(position!=0||postion0flag==false) {
final Button btn = holder.btnSqure;//final to call in inner class.
Point p = intPositionToPoint(position);
gm.gameScreen.board.addButtonToCell(btn, p.x, p.y);
holder.btnSqure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (gm.getUserClicks) {
Point p = intPositionToPoint(position);
gm.game(p, btn);
// gm.game(p, gm.cells[p.x][p.y].getButton());
}
}
});
}
if(position==0)
postion0flag=true;
return row;
}