日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
在Android開發(fā)中使用Gallery實(shí)現(xiàn)“多級(jí)聯(lián)動(dòng)”

之前我們?cè)蚰榻B過(guò)在Android中實(shí)現(xiàn)service動(dòng)態(tài)更新UI界面,在UI設(shè)計(jì)中需要利用很多圖庫(kù)相冊(cè)軟件,而Gallery 是國(guó)外一個(gè)免費(fèi)開源的、功能非常強(qiáng)大、有豐富的擴(kuò)展圖庫(kù)相冊(cè)軟件。本文將講解利用兩個(gè)Gallery實(shí)現(xiàn)類似多級(jí)聯(lián)動(dòng)的功能。

在谷城等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需策劃設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),全網(wǎng)營(yíng)銷推廣,外貿(mào)網(wǎng)站建設(shè),谷城網(wǎng)站建設(shè)費(fèi)用合理。

一個(gè)Gallery是歌曲專輯圖片,另一個(gè)Gallery是專輯的歌曲。滑動(dòng)專輯Gallery,下面的歌曲也會(huì)隨之發(fā)生變動(dòng)。

Gallery布局

主要的布局是有兩個(gè)相對(duì)布局+兩個(gè)Gallery組成的:

 
 
 
 
  1.  1: 
  2.  2: 
  3.  3:     android:layout_width="fill_parent" 
  4.  4:     android:layout_height="fill_parent"> 
  5.  5:     
  6.  6:     
  7.  7:         android:layout_width="fill_parent"
  8.  8:         android:layout_height="wrap_content"
  9.  9:         android:layout_alignParentTop="true"
  10. 10:         android:gravity="center_horizontal"
  11. 11:         android:spacing="16dp"
  12. 12:         android:unselectedAlpha="0.5"/>
  13. 13:     
  14. 14:     
  15. 15:         android:background="#FFF"
  16. 16:         android:layout_width="fill_parent"
  17. 17:         android:layout_height="30dp"
  18. 18:         android:layout_below="@id/gallery"
  19. 19:         android:layout_alignParentLeft="true"
  20. 20:         android:gravity="center_vertical"
  21. 21:         android:spacing="16dp"
  22. 22:         android:unselectedAlpha="0.5" />
  23. 23: 

Gallery的適配器

在android中適配器很好的實(shí)現(xiàn)了MVC思想,它很好的為某些組件提供了數(shù)據(jù)和view的實(shí)現(xiàn)。此處我們需要通過(guò)繼承BaseAdapter,實(shí)現(xiàn)兩個(gè)Gallery的適配器。

 
 
 
 
  1.  1: /**
  2.  2:  * 專輯
  3.  3:  * 
  4.  4:  * @author halzhang
  5.  5:  */
  6.  6: public class AlbumAdapter extends BaseAdapter {
  7.  7:  
  8.  8:     private Context context;
  9.  9:  
  10. 10:     private Cursor cursor;
  11. 11:  
  12. 12:     private Bitmap[] bitmaps;
  13. 13:  
  14. 14:     public AlbumAdapter(Context context) {
  15. 15:         this.context = context;
  16. 16:         this.cursor = context.getContentResolver().query(
  17. 17:                 MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, null, null,
  18. 18:                 MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
  19. 19:         bitmaps = new Bitmap[cursor.getCount()];
  20. 20:         initBitmaps();
  21. 21:     }
  22. 22:  
  23. 23:     /**
  24. 24:      * 初始化專輯封面圖片
  25. 25:      */
  26. 26:     private void initBitmaps() {
  27. 27:         if (cursor.moveToFirst()) {
  28. 28:             do {
  29. 29:                 bitmaps[cursor.getPosition()] = MusicUtils.getArtwork(context, -1, cursor
  30. 30:                         .getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID)));
  31. 31:             } while (cursor.moveToNext());
  32. 32:         }
  33. 33:     }
  34. 34:  
  35. 35:     public int getCount() {
  36. 36:         if (cursor != null) {
  37. 37:             return cursor.getCount();
  38. 38:         }
  39. 39:         return 0;
  40. 40:     }
  41. 41:  
  42. 42:     public Object getItem(int position) {
  43. 43:         return position;
  44. 44:     }
  45. 45:  
  46. 46:     public long getItemId(int position) {
  47. 47:         if (cursor != null) {
  48. 48:             cursor.moveToPosition(position);
  49. 49:             return cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID));
  50. 50:         }
  51. 51:         return 0;
  52. 52:     }
  53. 53:  
  54. 54:     public View getView(int position, View convertView, ViewGroup parent) {
  55. 55:         ImageView iv = new ImageView(context);
  56. 56:         iv.setLayoutParams(new Gallery.LayoutParams(100, 100));
  57. 57:         iv.setAdjustViewBounds(true);
  58. 58:         iv.setImageBitmap(bitmaps[position]);
  59. 59:         return iv;
  60. 60:     }
  61. 61:  
  62. 62: }
 
 
 
 
  1.  1: /**
  2.  2:  * 歌曲
  3.  3:  * 
  4.  4:  * @author halzhang
  5.  5:  */
  6.  6: public class AudioAdapter extends BaseAdapter {
  7.  7:  
  8.  8:     private Context context;
  9.  9:  
  10. 10:     private Cursor cursor;
  11. 11:     /**專輯ID*/
  12. 12:     private int albumId;
  13. 13:  
  14. 14:     public AudioAdapter(Context context, int albumId) {
  15. 15:         this.context = context;
  16. 16:         this.albumId = albumId;
  17. 17:         this.cursor = context.getContentResolver().query(
  18. 18:                 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
  19. 19:                 MediaStore.Audio.Media.ALBUM_ID + "=" + albumId, null,
  20. 20:                 MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
  21. 21:     }
  22. 22:  
  23. 23:     public int getCount() {
  24. 24:         if (cursor != null) {
  25. 25:             return cursor.getCount();
  26. 26:         }
  27. 27:         return 0;
  28. 28:     }
  29. 29:  
  30. 30:     public Object getItem(int position) {
  31. 31:         return position;
  32. 32:     }
  33. 33:  
  34. 34:     public long getItemId(int position) {
  35. 35:         if (cursor != null) {
  36. 36:             cursor.moveToPosition(position);
  37. 37:             return cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
  38. 38:         }
  39. 39:         return 0;
  40. 40:     }
  41. 41:  
  42. 42:     public View getView(int position, View convertView, ViewGroup parent) {
  43. 43:         cursor.moveToPosition(position);
  44. 44:         TextView t = new TextView(context);
  45. 45:         String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
  46. 46:         t.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
  47. 47:                 LayoutParams.WRAP_CONTENT));
  48. 48:         t.setText(title);
  49. 49:         t.setTextColor(Color.BLACK);
  50. 50:         return t;
  51. 51:     }
  52. 52:  
  53. 53:     /**
  54. 54:      * 當(dāng)專輯改變了,調(diào)用此方法更新adapter的數(shù)據(jù)
  55. 55:      * @param albumId 專輯ID
  56. 56:      */
  57. 57:     public void notifyDataSetChanged(int albumId) {
  58. 58:         this.cursor = context.getContentResolver().query(
  59. 59:                 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
  60. 60:                 MediaStore.Audio.Media.ALBUM_ID + "=" + albumId, null,
  61. 61:                 MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
  62. 62:         super.notifyDataSetChanged();
  63. 63:     }
  64. 64:  
  65. 65: }

Activity

 
 
 
 
  1.  1: public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
  2.  2:  
  3.  3:     private Gallery album;
  4.  4:  
  5.  5:     private Gallery audio;
  6.  6:  
  7.  7:     private AlbumAdapter albumAdapter;
  8.  8:  
  9.  9:     private AudioAdapter audioAdapter;
  10. 10:  
  11. 11:     @Override
  12. 12:     protected void onCreate(Bundle savedInstanceState) {
  13. 13:         super.onCreate(savedInstanceState);
  14. 14:         setContentView(R.layout.audio_player);
  15. 15:         setupViews();
  16. 16:     }
  17. 17:  
  18. 18:     // 個(gè)人習(xí)慣
  19. 19:     private void setupViews() {
  20. 20:         album = (Gallery) findViewById(R.id.gallery);
  21. 21:         audio = (Gallery) findViewById(R.id.gallery2);
  22. 22:  
  23. 23:         albumAdapter = new AlbumAdapter(this);
  24. 24:  
  25. 25:         album.setAdapter(albumAdapter);
  26. 26:  
  27. 27:         int aid = (int) albumAdapter.getItemId(0);
  28. 28:  
  29. 29:         audioAdapter = new AudioAdapter(this, aid);
  30. 30:         audio.setAdapter(audioAdapter);
  31. 31:  
  32. 32:         audio.setOnItemSelectedListener(this);
  33. 33:         album.setOnItemSelectedListener(this);
  34. 34:     }
  35. 35:  
  36. 36:     public void onItemSelected(AdapterView parent, View view, int position, long id) {
  37. 37:         if (parent == album) {
  38. 38:             // 專輯被選中
  39. 39:             int aid = (int) albumAdapter.getItemId(position);
  40. 40:             // 更新歌曲Gallery
  41. 41:             audioAdapter.notifyDataSetChanged(aid);
  42. 42:         } else if (parent == audio) {
  43. 43:             // TODO do something
  44. 44:         }
  45. 45:  
  46. 46:     }
  47. 47:  
  48. 48:     public void onNothingSelected(AdapterView parent) {
  49. 49:  
  50. 50:     }

好了,這就是我們介紹的在Android開發(fā)中使用Gallery實(shí)現(xiàn)“多級(jí)聯(lián)動(dòng)”的教程,謝謝大家。


網(wǎng)頁(yè)題目:在Android開發(fā)中使用Gallery實(shí)現(xiàn)“多級(jí)聯(lián)動(dòng)”
文章位置:http://www.5511xx.com/article/dpdjode.html