温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么利用Gallery实现多级联动

发布时间:2021-08-09 14:45:17 来源:亿速云 阅读:102 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关Android中怎么利用Gallery实现多级联动,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Gallery布局

主要的布局是有两个相对布局+两个Gallery组成的:

 1: <?xml version="1.0" encoding="utf-8"?>  2: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    3:     android:layout_width="fill_parent"    4:     android:layout_height="fill_parent">    5:     <!-- 专辑 -->  6:     <Gallery android:id="@+id/gallery"  7:         android:layout_width="fill_parent"  8:         android:layout_height="wrap_content"  9:         android:layout_alignParentTop="true" 10:         android:gravity="center_horizontal" 11:         android:spacing="16dp" 12:         android:unselectedAlpha="0.5"/> 13:     <!-- 歌曲 --> 14:     <Gallery android:id="@+id/gallery2" 15:         android:background="#FFF" 16:         android:layout_width="fill_parent" 17:         android:layout_height="30dp" 18:         android:layout_below="@id/gallery" 19:         android:layout_alignParentLeft="true" 20:         android:gravity="center_vertical" 21:         android:spacing="16dp" 22:         android:unselectedAlpha="0.5" /> 23: </RelativeLayout>

Gallery的适配器

在android中适配器很好的实现了MVC思想,它很好的为某些组件提供了数据和view的实现。此处我们需要通过继承BaseAdapter,实现两个Gallery的适配器。

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

Activity

 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:     }

看完上述内容,你们对Android中怎么利用Gallery实现多级联动有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI