1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
class MyActivity extends Activity implements OnListItemSelected
{
private static int[] BODY_PARTS = {
R.id.head_image,
R.id.body_image,
R.id.legs_image
};
private static int[][] PART_RESOURCES = {
// HEAD RESOURCES
{ R.drawable.head1, R.drawable.head2, R.drawable.head3 },
// BODY
{ R.drawable.body1, R.drawable.body2 },
// LEGS
{ R.drawable.legs1, R.drawable.legs2, R.drawable.legs3, R.drawable.legs4 }
};
private int state = 0; // head part
private int[] body_parts = new int[3]; // initial values
@Override
public void onCreate(Bundle savedState)
{
....
GridView gv = (GridView)findViewById(R.id.grid);
gv.setOnListItemSelected(this);
setupNewState();
}
private void setupNewState()
{
if (this.state < 3) {
PartAdapter adapter = new PartAdapter(this,R.layout.part_item,PART_RESOURCES[state]);
GridView gv = (GridView) findViewById(R.id.grid);
gv.setAdapter(adapter);
}
}
@Override
public void onListItemSelected(AdapterView view, int position, long id)
{
this.body_parts[this.state] = position;
ImageView bodyPartImageView = (ImageView)findViewById(BODY_PARTS[this.state]);
bodyPartImageView.setResourceDrawable(PART_RESOURCES[this.state][this.position]);
goNextState();
}
public void goNextState()
{
if (this.state == BODY_PARTS.length) return;
this.state ++;
setupNewState();
}
public void goPreviousState()
{
if (this.state == 0) return;
this.state --;
setupNewState();
}
} |
Partager