Android 使用Gallery实现图片“多级联动”
2010-08-11 10:47:00 来源:WEB开发网1: public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
2:
3: private Gallery album;
4:
5: private Gallery audio;
6:
7: private AlbumAdapter albumAdapter;
8:
9: private AudioAdapter audioAdapter;
10:
11: @Override
12: protected void onCreate(Bundle savedInstanceState) {
13: super.onCreate(savedInstanceState);
14: setContentView(R.layout.audio_player);
15: setupViews();
16: }
17:
18: // 个人习惯
19: private void setupViews() {
20: album = (Gallery) findViewById(R.id.gallery);
21: audio = (Gallery) findViewById(R.id.gallery2);
22:
23: albumAdapter = new AlbumAdapter(this);
24:
25: album.setAdapter(albumAdapter);
26:
27: int aid = (int) albumAdapter.getItemId(0);
28:
29: audioAdapter = new AudioAdapter(this, aid);
30: audio.setAdapter(audioAdapter);
31:
32: audio.setOnItemSelectedListener(this);
33: album.setOnItemSelectedListener(this);
34: }
35:
36: public void onItemSelected(AdapterView parent, View view, int position, long id) {
37: if (parent == album) {
38: // 专辑被选中
39: int aid = (int) albumAdapter.getItemId(position);
40: // 更新歌曲Gallery
41: audioAdapter.notifyDataSetChanged(aid);
42: } else if (parent == audio) {
43: // TODO do something
44: }
45:
46: }
47:
48: public void onNothingSelected(AdapterView parent) {
49:
50: }
更多精彩
赞助商链接