例如:YinYueBoFangQi
在java中的第一个子文件里新建包adapter包,entity和fragment包;
在res\layout中新建Layout XML File,取名music_item;新建Empty Activity文件,取Activity名Music,Layout名activity_music.
在adapter包中新建MusicListAdpter java class, 在entity包中新建Music java class .
public class Music {
public String title;
public String artist;
public String album;
public int length;
public Bitmap aLbumBmp;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lenovo.zy.pailie.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/main_local_tv"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:textSize="20sp"
android:text="本地音乐"
android:gravity="center"
/>
<TextView
android:id="@+id/main_online_tv"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:textSize="20sp"
android:text="在线音乐"
android:gravity="center"
/>
LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/main_vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
android.support.v4.view.ViewPager>
LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lenovo.zy.pailie.Music">
<TextView
android:id="@+id/music_title"
android:text="成都"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/music_artist"
android:text="赵雷-成都"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
LinearLayout>
这是虚拟的歌单,因为要先调试一下。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/music_item_bmp"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/music_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="成都"/>
<TextView
android:id="@+id/music_item_artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="赵雷"/>
LinearLayout>
模拟被调出的情况。由于考虑到ListView的排列,所以将两个TextView放在一个ImageView中,以方便排列。
package com.lenovo.zy.pailie;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.lenovo.zy.pailie.adapter.MusicPagerAdapter;
import com.lenovo.zy.pailie.fragment.LocalFragment;
import com.lenovo.zy.pailie.fragment.OnlineFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView localTV;
private TextView onlineTV;
private ViewPager viewPager;
private ListfragmentList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
localTV.setOnClickListener(this);
onlineTV.setOnClickListener(this);
LocalFragment localFragment=new LocalFragment();
OnlineFragment onlineFragment=new OnlineFragment();
fragmentList.add(localFragment);
fragmentList.add(onlineFragment);
MusicPagerAdapter pagerAdapter=new MusicPagerAdapter(getSupportFragmentManager(),fragmentList);
viewPager.setAdapter(pagerAdapter);
}
private void bindID() {
localTV=findViewById(R.id.main_local_tv);
onlineTV=findViewById(R.id.main_online_tv);
viewPager=findViewById(R.id.main_vp);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.main_local_tv:
break;
case R.id.main_online_tv:
break;
default:
break;
}
}
}
这里是定义几个类型,例如localTV,onlineTv,viewPager并绑定他们的ID和监听事件,以及他们的返回值。
package com.lenovo.zy.pailie.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lenovo.zy.pailie.R;
import com.lenovo.zy.pailie.entity.Music;
import java.util.List;
/**
* Created by pc on 2017/12/18.
*/
public class MusicListAdapter extends BaseAdapter{
private ListmusicList;
private Context context;
public MusicListAdapter(ListmusicList,Context context){
this.context=context;
this.musicList=musicList;
}
@Override
public int getCount() {
return musicList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view = null;
ViewHolder viewHolder=null;
if (convertView==null){
view= LayoutInflater.from(context).inflate(R.layout.music_item,null);
viewHolder=new ViewHolder();
viewHolder.titleTV=view.findViewById(R.id.music_item_title);
viewHolder.artistTV=view.findViewById(R.id.music_item_artist);
viewHolder.albumImgview=view.findViewById(R.id.music_item_bmp);
view.setTag(viewHolder);
}else {
view=convertView;
viewHolder=(ViewHolder)view.getTag();
}
Music music=musicList.get(i);
viewHolder.titleTV.setText(music.title);
viewHolder.artistTV.setText(music.artist+"-"+music.album);
if (music.aLbumBmp!=null){
viewHolder.albumImgview.setImageBitmap(music.aLbumBmp);
}else {
viewHolder.albumImgview.setImageResource(R.mipmap.default_cover);
}
return view;
}
class ViewHolder{
TextView titleTV;
TextView artistTV;
ImageView albumImgview;
}
}
创建全局变量
private void initListView() {
musicList = new ArrayList<>();
ContentResolver resolver = getActivity().getContentResolver();
Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
cursor.moveToFirst();
do {
Music m = new Music();
m.title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
m.album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
m.artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int albumID=cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
m.aLbumBmp=getAlbumArt(albumID);
musicList.add(m);
} while (cursor.moveToNext());
cursor.close();
MusicListAdapter adapter = new MusicListAdapter(musicList, getActivity());
localListView.setAdapter(adapter);
}
定义构造方法,改造适配器
private Bitmap getAlbumArt(int album_id) {
String mUriAlbums = "content://media/external/audio/albums";
String[] projection = new String[]{"album_art"};
Cursor cur = getActivity().getContentResolver().query(Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)), projection, null, null, null);
String album_art = null;
if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
cur.moveToNext();
album_art = cur.getString(0);
}
cur.close();
Bitmap bm = null;
if (album_art != null) {
bm = BitmapFactory.decodeFile(album_art);
} else {
bm = BitmapFactory.decodeResource(getResources(), R.mipmap.default_cover);
}
return bm;
}