码迷,mamicode.com
首页 > 移动开发 > 详细

Android 音乐播放器--歌曲列表的显示

时间:2015-01-26 13:36:29      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

歌曲列表是靠ListView实现的,在这里我给每首歌曲定义了一个类,并自定义了一个ArrayAdapter

添加Song类

Song类中包含了 标题 专辑 专辑图片信息等

 1 public class Song {
 2     String title;
 3     String artist;
 4     String album;
 5     String Data;
 6     String albumn_art;
 7 
 8     public Song(String title, String artist, String album) {
 9         this.title = title;
10         this.artist = artist;
11         this.album = album;
12     }
13     public Song(){
14 
15     }
16 
17     public String getAlbumn_art() {
18         return albumn_art;
19     }
20 
21     public void setAlbumn_art(String albumn_art) {
22         this.albumn_art = albumn_art;
23     }
24 
25     public String getData() {
26         return Data;
27     }
28 
29     public void setData(String data) {
30         Data = data;
31     }
32 
33     public String getTitle() {
34         return title;
35     }
36 
37     public void setTitle(String title) {
38         this.title = title;
39     }
40 
41     public String getArtist() {
42         return artist;
43     }
44 
45     public void setArtist(String artist) {
46         this.artist = artist;
47     }
48 
49     public String getAlbum() {
50         return album;
51     }
52 
53     public void setAlbum(String album) {
54         this.album = album;
55     }
56 }

用于显示歌曲列表每一项的布局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="horizontal"
 4     android:layout_width="match_parent"
 5     android:layout_marginRight="10dp"
 6     android:layout_height="60dp">
 7     <!-- 歌曲的序列号 -->
 8     <TextView
 9         android:id="@+id/song_sort"
10         android:layout_width="wrap_content"
11         android:minWidth="45dp"
12         android:maxWidth="45dp"
13         android:layout_height="60dp"
14         android:text="1"
15         android:maxLines="1"
16         android:layout_gravity="center"
17         android:gravity="center"
18         android:textSize="22dp"
19         android:layout_weight="1"/>
20     <LinearLayout
21         android:layout_width="0dp"
22         android:layout_height="60dp"
23         android:layout_weight="20"
24         android:orientation="vertical"
25         >
26         <!-- 标题 -->
27         <TextView
28             android:id="@+id/song_title"
29             android:layout_weight="5"
30             android:layout_width="wrap_content"
31             android:layout_height="0dp"
32             android:maxLines="1"
33             android:gravity="center_vertical"
34             android:textSize="30dp"
35             android:textColor="#333333"
36             android:text="heihei"/>
37         <!-- 专辑与歌手信息 -->
38         <TextView
39             android:id="@+id/song_info"
40             android:layout_weight="3"
41             android:gravity="center_vertical"
42             android:maxLines="1"
43             android:layout_gravity="center_vertical"
44             android:layout_width="wrap_content"
45             android:layout_height="0dp"
46             android:textSize="14dp"
47             android:text="hahfdgdfhfghhfga"/>
48 
49         </LinearLayout>
50 
51 </LinearLayout>

SongAdapter

 1 package com.example.agmcs.musicplayer;
 2 
 3 import android.content.Context;
 4 import android.content.Intent;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.ArrayAdapter;
 9 import android.widget.TextView;
10 
11 import org.w3c.dom.Text;
12 
13 import java.util.List;
14 
15 
16 public class SongAdapter extends ArrayAdapter<Song> {
17     private int resId;
18 
19     public SongAdapter(Context context, int resource, List<Song> objects) {
20         super(context, resource, objects);
21         this.resId = resource;
22     }
23 
24     @Override
25     public View getView(int position, View convertView, ViewGroup parent) {
26         Song song = getItem(position);
27         View view;
28         ViewHolder holder;
29         if(convertView == null){
30             view = LayoutInflater.from(getContext()).inflate(resId,null);
31             holder = new ViewHolder();
32             holder.info = (TextView)view.findViewById(R.id.song_info);
33             holder.title = (TextView)view.findViewById(R.id.song_title);
34             holder.sort = (TextView)view.findViewById(R.id.song_sort);
35             view.setTag(holder);
36         }else{
37             view = convertView;
38             holder = (ViewHolder)view.getTag();
39         }
40         holder.title.setText(song.getTitle());
41         holder.info.setText(song.getArtist() + " - " + song.getAlbum());
42         holder.sort.setText(String.valueOf(position+1));
43         return view;
44     }
45 
46     //优化ArrayAdapter
47     class ViewHolder{
48         TextView title;
49         TextView info;
50         TextView sort;
51     }
52 }

ListView的初始化

listview 是在Fragment中显示的

 1 package com.example.agmcs.musicplayer;
 2 
 3 import android.app.Activity;
 4 import android.app.ListFragment;
 5 import android.content.BroadcastReceiver;
 6 import android.content.Context;
 7 import android.content.Intent;
 8 import android.content.IntentFilter;
 9 import android.net.Uri;
10 import android.os.Bundle;
11 import android.app.Fragment;
12 import android.util.Log;
13 import android.view.LayoutInflater;
14 import android.view.View;
15 import android.view.ViewGroup;
16 import android.widget.AdapterView;
17 import android.widget.ListView;
18 import android.widget.Toast;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 
23 
24 /**
25  * A simple {@link Fragment} subclass.
26  * Activities that contain this fragment must implement the
27  * {@link MusicListFrag.OnFragmentInteractionListener} interface
28  * to handle interaction events.
29  * Use the {@link MusicListFrag#newInstance} factory method to
30  * create an instance of this fragment.
31  */
32 public class MusicListFrag extends Fragment implements AdapterView.OnItemClickListener {
33     private ListView list;
34     private SongAdapter adapter;
35 
36 
37     @Override
38     public View onCreateView(LayoutInflater inflater, ViewGroup container,
39                              Bundle savedInstanceState) {
40         // Inflate the layout for this fragment
41         View view = inflater.inflate(R.layout.fragment_music_list, container, false);
42 
43         list = (ListView)view.findViewById(R.id.music_listview);
44         return view;
45     }
46 
47     @Override
48     public void onActivityCreated(Bundle savedInstanceState) {
49         super.onActivityCreated(savedInstanceState);
50         adapter = new SongAdapter(getActivity(),R.layout.music_item,PlayerServer.songList);
51         list.setAdapter(adapter);
52         list.setOnItemClickListener(this);
53     }
54 
55     @Override
56     public void onResume() {
57         super.onResume();
58         IntentFilter intentFilter = new IntentFilter(PlayerServer.updateBroadcast);
59         UpdateReceiver receiver = new UpdateReceiver();
60         getActivity().registerReceiver(receiver,intentFilter);
61     }
62 
63     public class UpdateReceiver extends BroadcastReceiver{
64         @Override
65         //接受Service发出的刷新完毕广播后,刷新下Listview
66         //这里不能用adapter.notifyDateChange 因为songList完全是另一个内存
67         public void onReceive(Context context, Intent intent) {
68             adapter = new SongAdapter(getActivity(),R.layout.music_item,PlayerServer.songList);
69             list.setAdapter(adapter);
70             Log.d("hi","???");
71         }
72     }
73 
74     @Override
75     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
76         //当点击时播放这首歌
77         Intent i = new Intent(getActivity(), PlayerServer.class);
78         i.putExtra("MSG",Constant.PlayerMsg.PLAY_MSG);
79         i.putExtra("position",position);
80         getActivity().startService(i);
81     }
82 }

 

Android 音乐播放器--歌曲列表的显示

标签:

原文地址:http://www.cnblogs.com/agmcs/p/4250021.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!