实现效果如图:
FooterView:
package com.iwanghang.drmplayer; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.iwanghang.drmplayer.adapter.MyMusicListAdapter; import com.iwanghang.drmplayer.adapter.NetMusicListAdapter; import com.iwanghang.drmplayer.utils.AppUtils; import com.iwanghang.drmplayer.utils.Constant; import com.iwanghang.drmplayer.utils.SearchMusicUtils; import com.iwanghang.drmplayer.vo.Mp3Info; import com.iwanghang.drmplayer.vo.SearchResult; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; import java.io.IOException; import java.util.ArrayList; public class NetMusicListFragment extends Fragment implements OnItemClickListener,OnClickListener { private ListView listView_net_music; //private ArrayList<Mp3Info> mp3Infos; private LinearLayout load_layout; private LinearLayout ll_search_btn_container; private LinearLayout ll_search_container; private ImageButton ib_search_btn; private EditText et_search_content; private NetMusicListAdapter netMusicListAdapter; //存放 网络音乐 的集合 private ArrayList<SearchResult> searchResults = new ArrayList<SearchResult>(); private int page = 1;//搜索音乐的页码 private MainActivity mainActivity; //private boolean isPause = false;//歌曲播放中的暂停状态 private int position = 0;//当前播放的位置,提供给PlayActivity //onAttach(),当fragment被绑定到activity时被调用(Activity会被传入.). @Override public void onAttach(Context context) { super.onAttach(context); //mainActivity = (MainActivity) context; mainActivity = (MainActivity) getActivity(); } public static NetMusicListFragment newInstance() { NetMusicListFragment net = new NetMusicListFragment(); return net; } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //UI组件初始化 View view = inflater.inflate(R.layout.net_music_list_layout,null); //item listView_net_music = (ListView) view.findViewById(R.id.listView_net_music); //findViewById load_layout = (LinearLayout) view.findViewById(R.id.load_layout); ll_search_btn_container = (LinearLayout) view.findViewById(R.id.ll_search_btn_container); ll_search_container = (LinearLayout) view.findViewById(R.id.ll_search_container); ib_search_btn = (ImageButton) view.findViewById(R.id.ib_search_btn); et_search_content = (EditText) view.findViewById(R.id.et_search_content); //Item点击事件监听 listView_net_music.setOnItemClickListener(this); //按钮点击事件监听 ll_search_btn_container.setOnClickListener(this); ib_search_btn.setOnClickListener(this); loadNetData();//加载网络音乐 return view; } private void loadNetData() { load_layout.setVisibility(View.VISIBLE); //加载网络音乐的异步任务 new LoadNetDataTask().execute(Constant.BAIDU_URL + Constant.BAIDU_DAYHOT); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.ll_search_btn_container: ll_search_btn_container.setVisibility(View.GONE); ll_search_container.setVisibility(View.VISIBLE); break; case R.id.ib_search_btn: //搜索事件 searchMusic(); break; } } //搜索音乐 private void searchMusic() { //隐藏键盘 AppUtils.hideInputMethod(et_search_content); ll_search_btn_container.setVisibility(View.VISIBLE); ll_search_container.setVisibility(View.GONE); //获取输入的文字 String key = et_search_content.getText().toString(); if (TextUtils.isEmpty(key)){//如果为空,则,Toast提示 Toast.makeText(mainActivity,"请输入歌手或歌词",Toast.LENGTH_SHORT).show(); return; } load_layout.setVisibility(View.VISIBLE);//加载layout效果.显示 //填充item 使用SearchMusicUtils搜索音乐工具类,并,使用观察者设计模式,自己回调,自己监听 //SearchMusicUtils. } //加载网络音乐的异步任务 //Android1.5提供了 工具类 android.os.AsyncTask,它使创建异步任务变得更加简单,不再需要编写任务线程和Handler实例即可完成相同的任务。 class LoadNetDataTask extends AsyncTask<String,Integer,Integer>{ //onPreExecute方法用于在执行后台任务前做一些UI操作 @Override protected void onPreExecute() { super.onPreExecute(); load_layout.setVisibility(View.VISIBLE);//加载layout.显示 listView_net_music.setVisibility(View.GONE);//item.隐藏 searchResults.clear();//搜索结果.清理 } //doInBackground方法内部执行后台任务,不可在此方法内修改UI @Override protected Integer doInBackground(String... params) { String url = params[0]; try { //使用Jsoup组件请求网络,并解析音乐数据 Document doc = Jsoup.connect(url).userAgent(Constant.USER_AGENT).timeout(6*1000).get(); //</span></span><span class="song-title " style='width: 240px;'><a href="/song/121353608" target="_blank" title="刘珂矣半壶纱" data-film="null">半壶纱</a></span><span class="singer" style="width: 240px;" > <span class="author_list" title="刘珂矣"> //<a hidefocus="true" href="/artist/132632388">刘珂矣</a> </span> //</span></span><span class="song-title " style='width: 240px;'><a href="/song/264506450" target="_blank" title="韩磊千年一般若" data-film="null">千年一般若</a><div class="extra-info"><span class="appendix">影视原声</span></div></span><span class="singer" style="width: 240px;" > <span class="author_list" title="韩磊"> //<a hidefocus="true" href="/artist/1302">韩磊</a> </span> //从doc分析以上html代码;把所有span标签下song-title查找出来,存在songTitles集合中;即,歌名集合; //从doc分析以上html代码;把所有span标签下author_list查找出来,存在artists集合中;即,歌手集合; Elements songTitles = doc.select("span.song-title"); Elements artists = doc.select("span.author_list"); //System.out.println(artists); for (int i=0;i<songTitles.size();i++){ SearchResult searchResult = new SearchResult(); //System.out.println("@searchResult : " + searchResult); //<a href="/song/121353608" //<a href="/song/264506450" //a链接,存在urls集合中;即,歌曲url集合; //a链接,第一个a连接,href属性的值;即,最终的url; //a链接,第一个a连接,text(a链接的内容,例:>半壶纱<,半壶纱就是a链接的内容);即,最终的歌名; Elements urls = songTitles.get(i).getElementsByTag("a"); //System.out.println("@urls : " + urls); searchResult.setUrl(urls.get(0).attr("href"));//设置最终的url searchResult.setMusicName(urls.get(0).text());//设置最终的歌名 //a链接,存在urls集合中;即,歌曲url集合; Elements artistElements = artists.get(i).getElementsByTag("a"); //System.out.println("@artistElements : " + artistElements); searchResult.setArtist(artistElements.get(0).text());//设置最终的歌手 searchResult.setAlbum("热歌榜");//设置最终的专辑 System.out.println("@searchResult : " + searchResult); searchResults.add(searchResult);//把最终的所有信息,添加到集合 } System.out.println("@searchResults : " + searchResults); //System.out.println("@songTitles.size() : " + searchResults.size()); } catch (IOException e) { e.printStackTrace(); return -1; } return 1; } //onPostExecute方法用于在执行完后台任务后更新UI,显示结果 @Override protected void onPostExecute(Integer result) { super.onPostExecute(result); if (result==1){ netMusicListAdapter = new NetMusicListAdapter(mainActivity,searchResults); //System.out.println(searchResults); listView_net_music.setAdapter(netMusicListAdapter); listView_net_music.addFooterView(LayoutInflater.from(mainActivity).inflate(R.layout.footviwe_layout,null)); } load_layout.setVisibility(View.GONE); listView_net_music.setVisibility(View.VISIBLE); } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }
package com.iwanghang.drmplayer.adapter; import android.content.Context; import android.graphics.Bitmap; 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.iwanghang.drmplayer.R; import com.iwanghang.drmplayer.utils.MediaUtils; import com.iwanghang.drmplayer.vo.Mp3Info; import com.iwanghang.drmplayer.vo.SearchResult; import java.util.ArrayList; /** * 自定义的音乐列表适配器(网络) * 为了方便扩展,因为之前没有考虑到显示专辑封面 * Created by iwanghang on 30/4/16. */ public class NetMusicListAdapter extends BaseAdapter{ private Context ctx; //上下文对象引用 private ArrayList<SearchResult> searchResults;//存放SearchResult引用的集合 private SearchResult searchResult;//SearchResult对象引用 //private int pos = -1; //列表位置 /** * 构造函数 * @param ctx 上下文 * @param searchResults 集合对象 */ public NetMusicListAdapter(Context ctx, ArrayList<SearchResult> searchResults){ this.ctx = ctx; this.searchResults = searchResults; //System.out.println("MyMusicListAdapter.java #0 : ctx = " + ctx + ",mp3Infos = " + mp3Infos.size()); } public ArrayList<SearchResult> searchResults() { System.out.println("NetMusicListAdapter.java #1 : public ArrayList<SearchResult> searchResults() {"); return searchResults; } public void setSearchResults(ArrayList<SearchResult> searchResults) { System.out.println("NetMusicListAdapter.java #2 : public void setMp3Infos(ArrayList<SearchResult> searchResults) {"); this.searchResults = searchResults; } @Override public int getCount() { //System.out.println("NetMusicListAdapter.java #3 : public int getCount() {" + mp3Infos.size()); //return mp3Infos.size(); return searchResults.size(); } @Override public Object getItem(int position) { System.out.println("NetMusicListAdapter.java #4 : public Object getItem(int position) {"); return searchResults.get(position); } @Override public long getItemId(int position) { //System.out.println("NetMusicListAdapter.java #5 : public long getItemId(int position) {"); return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //System.out.println("NetMusicListAdapter.java #6 : public View getView "); ViewHolder vh; if(convertView==null){ //vh = new ViewHolder(); convertView = LayoutInflater.from(ctx).inflate(R.layout.item_net_music_list,null); vh = new ViewHolder(); vh.textView1_title = (TextView) convertView.findViewById(R.id.textView1_title); vh.textView2_singer = (TextView) convertView.findViewById(R.id.textView2_singer); //vh.textView3_time = (TextView) convertView.findViewById(R.id.textView3_time); //vh.imageView1_ablum = (ImageView) convertView.findViewById(R.id.imageView1_ablum); //System.out.println("NetMusicListAdapter.java #7 : textView1_title = " + vh.textView1_title); convertView.setTag(vh);//表示给View添加一个格外的数据, }else { vh = (ViewHolder)convertView.getTag();//通过getTag的方法将数据取出来 } SearchResult searchResult = searchResults.get(position); vh.textView1_title.setText(searchResult.getMusicName());//显示标题 vh.textView2_singer.setText(searchResult.getArtist());//显示歌手 //vh.textView3_time.setText(MediaUtils.formatTime(mp3Info.getDuration()));//显示时长 //获取专辑封面图片 //Bitmap albumBitmapItem = MediaUtils.getArtwork(ctx,mp3Info.getId(),mp3Info.getAlbumId(),true,true); //System.out.println("NetMusicListAdapter.java #8 : albumBitmapItem = " + albumBitmapItem.getConfig()); //改变播放界面专辑封面图片 //vh.imageView1_ablum.setImageBitmap(albumBitmapItem); //vh.imageView1_ablum.setImageResource(R.mipmap.music); return convertView; } /** * 定义一个内部类 * 声明相应的控件引用 */ static class ViewHolder{ //所有控件对象引用 TextView textView1_title;//标题 TextView textView2_singer;//歌手 //TextView textView3_time;//时长 //ImageView imageView1_ablum;//专辑封面图片 } }
package com.iwanghang.drmplayer.vo; /** * Created by iwanghang on 16/4/29. * 搜索音乐的对象 */ public class SearchResult { private String musicName; private String url; private String artist; private String album; private long size;//大小 //set方法,get方法 public String getMusicName() { return musicName; } public void setMusicName(String musicName) { this.musicName = musicName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } @Override public String toString() { return "SearchResult{" + "musicName='" + musicName + '\'' + ", url='" + url + '\'' + ", artist='" + artist + '\'' + ", album='" + album + '\'' + ", size=" + size + '}'; } }
footview_layout.xml如下:
<?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/textView4" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" android:gravity="center" android:textSize="10sp" android:textColor="#a6a8a8" android:text="已经到最后了"> </TextView>