让我们从我是 android 新手开始。
我有一个 GridView 设置为网格,我用它来代表我正在制作的一个小棋盘游戏应用程序的棋盘。我已经为我的 GridView 设置了一个“setOnItemClickListener”,它至少可以为 Toast 设置一个“setOnItemClickListener”,并设置我放在那里进行测试的 ImageResource。
我不知道如何从 ClickListener 内的其他类调用我的方法来控制触摸发生时发生的情况。似乎没有名称或任何可与其他方法一起使用的东西。当我尝试将我的代码放入 setOnItemClickListener 时,但我收到一条警告,说我的对象(我想从中调用方法的类)必须是最终的,才能在另一个方法的内部类中使用。
我无法将对象更改为最终对象,因此我需要一种方法来运行一个小游戏循环,该循环将通过与 setOnItemClickListener 通信来调用我需要的其他类的对象上的所有方法。
这是我的具有侦听器的代码部分:
public View play(GridView gv) {
boolean p1turn = true;
while (gameOver == false) {
gv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Toast.makeText(ctxt, "" + position, Toast.LENGTH_SHORT).show();
ImageView img = (ImageView)v;
img.setImageResource(R.drawable.paper1select);
}
});
System.out.println("Why no get here??");
return(gv);//IDK why but this is NEVER reached, almost like the oICL is an infinite loop???
}
return gv;
}
侦听器内的 Toast 和两条 ImageView img 行仅用于测试它是否可以工作以及是否适用于这些任务。不过,我需要一种在我的代码的其他地方使用监听器的方法。
例如,在第一次触摸时,我想调用我创建的一个方法,该方法将确定所选项目是否是符合条件的移动单元,如果是,则将该位置的图像与新图像交换。所以它看起来像我想象的这样 - gv.onTouchEvent1.checkPiece(player1, position); - checkPiece 将从 ClickListener 获取位置并执行其功能。
任何 setOnItemClickListener 教程建议都会很好。
----------------用我的解决方案编辑------------------------------ --
我能够使它工作的唯一方法是在内部使用一个计数器,onItemClick...
这将允许我根据使用if
语句和循环的计数来控制调用哪个方法。这仍然是一项正在进行的工作,所以我稍后会删除一些垃圾,但这基本上显示了我需要的内容:
public class MainActivity extends Activity {
/*
* count used to keep track of which step of game-play we are on. pNum used
* to track which player's turn it is.
*/
protected static int count = 0, pNum = 0, round = 0;
// Used to keep know that a step is done, in conjunction with count,
// redundant I believe. TRUE indicates "done".
protected static boolean choosePiece = false, chooseEmpty = false,
verifyEmpty = false, selectEnemy = false, verifyEnemy = false;
// Used to track selected piece location.
protected static int selectedLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageAdapter iA = new ImageAdapter(this);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(iA);
final GameplayController gc = new GameplayController(this);
// Create board model, add units to player's list.
final UnitArray gameBoardModel = new UnitArray();
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
while (count <= 6) {
System.out.println("While loop cycle: " + round);
/*
* First IF is for selecting piece to move.
*/
if (count == 0) {
System.out.println("IF 0 WAS HIT");
System.out.println("It is player " + pNum
+ "s turn, round " + round);
choosePiece = gc.choosePiece(position, gameBoardModel,
(ImageView) v, pNum);
if (choosePiece == true) {
count++;
}
selectedLocation = position;
break;
}
/*
* Second IF is for selecting blank space to move to.
*/
if (count == 1 && choosePiece == true
&& chooseEmpty == false) {
System.out.println("IF 1 WAS HIT");
chooseEmpty = gc.chooseEmpty(position, gameBoardModel,
(ImageView) v);
if (chooseEmpty == true) {
count++;
}
break;
}
/*
* Third IF is for verifying the blank selected and moving
* piece.
*/
if (count == 2 && verifyEmpty == false
&& choosePiece == true && chooseEmpty == true) {
System.out.println("IF 2 WAS HIT");
verifyEmpty = gc.checkEmptyChoice(position);
if (verifyEmpty == true) {
gc.moveToEmpty(position, gameBoardModel,
(ImageView) v, selectedLocation);
count++;
iA.notifyDataSetChanged();
selectedLocation = position;
}
break;
}
/*
* Fourth IF is for selecting an enemy unit to attack.
*/
if (count == 3 && selectEnemy == false
&& verifyEmpty == true && choosePiece == true
&& chooseEmpty == true) {
System.out.println("IF 3 WAS HIT");
selectEnemy = gc.selectEnemy(position, gameBoardModel,
(ImageView) v);
if (selectEnemy == true) {
count++;
}
break;
}
/*
* Fifth IF is for verifying enemy unit to attack and then
* attacking.
*/
if (count == 4 && verifyEnemy == false
&& selectEnemy == true && verifyEmpty == true
&& choosePiece == true && chooseEmpty == true) {
System.out.println("IF 4 WAS HIT");
verifyEnemy = gc.verifyEnemySelected(position);
if (verifyEnemy) {
/*
* For clarity, position is enemy being attacked,
* selectedLocation is position of attacking unit.
*/
gc.attackEnemy(position, gameBoardModel,
(ImageView) v, selectedLocation);
// RESET FOR NEXT PLAYER TURN.
iA.notifyDataSetChanged();
if (pNum == 0) {
pNum = 1;
} else {
pNum = 0;
}
iA.notifyDataSetChanged();
count = 0;
choosePiece = false;
chooseEmpty = false;
verifyEmpty = false;
selectEnemy = false;
verifyEnemy = false;
round++;
}
break;
}
}
}
});
}
...
对我来说,在onItemClick
. 我最初的意图是调用一个几乎包含我最终放入 onClick 的所有内容的方法,因为我无法弄清楚如何仅在其位置上区分触摸事件。