简体   繁体   中英

Unsure how to structure by Android application

I'm a newbie to Android development, and am currently developing a game that consists of a grid with images. So my main Game activity loads a GridView, which in turn is populated by a custom BaseAdapter I've extended. The idea is that when the user clicks an image that the grid is redrawn, then the AI makes a move and the grid is redrawn again etc. So in my BaseAdapter, when I create an ImageView I'm setting a custom OnClickListener that performs some calculations and then invokes BaseAdapter.notifyDataSetChanged() to update the GridView after.

However, I'm unsure now as to where I should locate the logic for the AI's "move", and how can this be invoked? Instead of having a custom OnClickListener class, should my main Game activity instead implement OnClickListener?

Thanks

If your game is based on clicks on the GridView , I don't see anything wrong in keep the OnClickListener there. You might want to simplify the code inside the listener, though, and let it just call a function nextMove() in your Game class to handle the game logic.

Note that this class would not be your main Activity either (I would rename the Activity to something a little more descriptive than "Game"). Try to separate as much as possible the UI from the game logic. Obviously, the logic needs to respond to UI events, and change the UI accordingly, but the interrelation should stop there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM