parent
1a50e6c217
commit
203f146574
@ -0,0 +1,73 @@
|
||||
package com.example.musicplayer.adapter;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.musicplayer.R;
|
||||
import com.example.musicplayer.entiy.SeachSong;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by 残渊 on 2018/11/21.
|
||||
*/
|
||||
|
||||
public class SearchContentAdapter extends RecyclerView.Adapter<SearchContentAdapter.ViewHolder> {
|
||||
private ArrayList<SeachSong.DataBean> mSongListBeans;
|
||||
private static ItemClick mItemClick;
|
||||
|
||||
public static void setItemClick(ItemClick itemClick){
|
||||
mItemClick = itemClick;
|
||||
}
|
||||
|
||||
public SearchContentAdapter(ArrayList<SeachSong.DataBean> songListBeans){
|
||||
mSongListBeans = songListBeans;
|
||||
}
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.recycler_song_search_item,parent,false);
|
||||
ViewHolder viewHolder = new ViewHolder(view);
|
||||
return viewHolder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
|
||||
SeachSong.DataBean songListBean = mSongListBeans.get(position);
|
||||
holder.artistTv.setText(songListBean.getSinger());
|
||||
holder.titleTv.setText(songListBean.getName());
|
||||
holder.mItemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
mItemClick.onClick(position);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return mSongListBeans.size();
|
||||
}
|
||||
|
||||
class ViewHolder extends RecyclerView.ViewHolder{
|
||||
TextView titleTv;
|
||||
TextView artistTv;
|
||||
View mItemView;
|
||||
|
||||
public ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
titleTv = itemView.findViewById(R.id.tv_title);
|
||||
artistTv = itemView.findViewById(R.id.tv_artist);
|
||||
mItemView = itemView;
|
||||
}
|
||||
}
|
||||
|
||||
public interface ItemClick{
|
||||
void onClick(int position);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.example.musicplayer.contract;
|
||||
|
||||
import com.example.musicplayer.entiy.SeachSong;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by 残渊 on 2018/11/21.
|
||||
*/
|
||||
|
||||
public interface ISearchContentContract {
|
||||
interface Model{
|
||||
void search(String seek); //搜索
|
||||
}
|
||||
|
||||
interface View{
|
||||
String getSeekContent(); //获得搜索的内容
|
||||
void setSongsList(ArrayList<SeachSong.DataBean> songListBeans); //显示歌曲列表
|
||||
void showError();
|
||||
}
|
||||
interface Presenter{
|
||||
void search(String seek); //搜索
|
||||
void searchSuccess(ArrayList<SeachSong.DataBean> songListBeans); //搜索成功
|
||||
void searchError(); //搜索失败
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
package com.example.musicplayer.entiy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by 残渊 on 2018/11/21.
|
||||
*/
|
||||
|
||||
public class SeachSong {
|
||||
|
||||
|
||||
/**
|
||||
* result : SUCCESS
|
||||
* code : 200
|
||||
* data : [{"id":"452804665","name":"告白气球","singer":"Mc梦柯","pic":"https://api.bzqll.com/music/netease/pic?id=452804665&key=579621905","lrc":"https://api.bzqll.com/music/netease/lrc?id=452804665&key=579621905","url":"https://api.bzqll.com/music/netease/url?id=452804665&key=579621905"},{"id":"536570450","name":"魔术与歌曲:告白气球","singer":"周杰伦/蔡威泽","pic":"https://api.bzqll.com/music/netease/pic?id=536570450&key=579621905","lrc":"https://api.bzqll.com/music/netease/lrc?id=536570450&key=579621905","url":"https://api.bzqll.com/music/netease/url?id=536570450&key=579621905"},{"id":"864255512","name":"告白气球","singer":"初见","pic":"https://api.bzqll.com/music/netease/pic?id=864255512&key=579621905","lrc":"https://api.bzqll.com/music/netease/lrc?id=864255512&key=579621905","url":"https://api.bzqll.com/music/netease/url?id=864255512&key=579621905"},{"id":"454924401","name":"告白气球","singer":"张穆庭","pic":"https://api.bzqll.com/music/netease/pic?id=454924401&key=579621905","lrc":"https://api.bzqll.com/music/netease/lrc?id=454924401&key=579621905","url":"https://api.bzqll.com/music/netease/url?id=454924401&key=579621905"},{"id":"1301125157","name":"告白气球","singer":"文景熙","pic":"https://api.bzqll.com/music/netease/pic?id=1301125157&key=579621905","lrc":"https://api.bzqll.com/music/netease/lrc?id=1301125157&key=579621905","url":"https://api.bzqll.com/music/netease/url?id=1301125157&key=579621905"},{"id":"1300594463","name":"告白气球","singer":"张阿辉","pic":"https://api.bzqll.com/music/netease/pic?id=1300594463&key=579621905","lrc":"https://api.bzqll.com/music/netease/lrc?id=1300594463&key=579621905","url":"https://api.bzqll.com/music/netease/url?id=1300594463&key=579621905"},{"id":"509726593","name":"告白气球 (卡拉偶客版)","singer":"SNH48","pic":"https://api.bzqll.com/music/netease/pic?id=509726593&key=579621905","lrc":"https://api.bzqll.com/music/netease/lrc?id=509726593&key=579621905","url":"https://api.bzqll.com/music/netease/url?id=509726593&key=579621905"},{"id":"863515406","name":"告白气球","singer":"战小二吖","pic":"https://api.bzqll.com/music/netease/pic?id=863515406&key=579621905","lrc":"https://api.bzqll.com/music/netease/lrc?id=863515406&key=579621905","url":"https://api.bzqll.com/music/netease/url?id=863515406&key=579621905"}]
|
||||
*/
|
||||
|
||||
private String result;
|
||||
private int code;
|
||||
private List<DataBean> data;
|
||||
|
||||
public String getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(String result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public List<DataBean> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(List<DataBean> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static class DataBean {
|
||||
/**
|
||||
* id : 452804665
|
||||
* name : 告白气球
|
||||
* singer : Mc梦柯
|
||||
* pic : https://api.bzqll.com/music/netease/pic?id=452804665&key=579621905
|
||||
* lrc : https://api.bzqll.com/music/netease/lrc?id=452804665&key=579621905
|
||||
* url : https://api.bzqll.com/music/netease/url?id=452804665&key=579621905
|
||||
*/
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private String singer;
|
||||
private String pic;
|
||||
private String lrc;
|
||||
private String url;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSinger() {
|
||||
return singer;
|
||||
}
|
||||
|
||||
public void setSinger(String singer) {
|
||||
this.singer = singer;
|
||||
}
|
||||
|
||||
public String getPic() {
|
||||
return pic;
|
||||
}
|
||||
|
||||
public void setPic(String pic) {
|
||||
this.pic = pic;
|
||||
}
|
||||
|
||||
public String getLrc() {
|
||||
return lrc;
|
||||
}
|
||||
|
||||
public void setLrc(String lrc) {
|
||||
this.lrc = lrc;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,26 @@
|
||||
package com.example.musicplayer.https;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import retrofit2.CallAdapter;
|
||||
import retrofit2.Converter;
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
/**
|
||||
* Created by 残渊 on 2018/10/26.
|
||||
*/
|
||||
|
||||
public class RetrofitHelp {
|
||||
private static okhttp3.OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS);
|
||||
private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
|
||||
private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJava2CallAdapterFactory.create();
|
||||
|
||||
public RetrofitHelp(){
|
||||
|
||||
}
|
||||
public static RetrofitHelp getInstance(){
|
||||
return new RetrofitHelp();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.example.musicplayer.https.api;
|
||||
|
||||
import com.example.musicplayer.entiy.SeachSong;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import retrofit2.http.Field;
|
||||
import retrofit2.http.FormUrlEncoded;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.POST;
|
||||
import retrofit2.http.Path;
|
||||
import retrofit2.http.Query;
|
||||
|
||||
/**
|
||||
* Created by 残渊 on 2018/11/21.
|
||||
*/
|
||||
|
||||
public interface SearchApi {
|
||||
@GET("search?key=579621905&type=song&limit=30&offset=0")
|
||||
Observable<SeachSong> search(@Query("s") String seek);
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.example.musicplayer.model;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.example.musicplayer.contract.ISearchContentContract;
|
||||
import com.example.musicplayer.entiy.SeachSong;
|
||||
import com.example.musicplayer.https.NetWork;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* Created by 残渊 on 2018/11/21.
|
||||
*/
|
||||
|
||||
public class SearchContentModel implements ISearchContentContract.Model {
|
||||
private static final String TAG="SearchContentModel";
|
||||
private ISearchContentContract.Presenter mPresenter;
|
||||
|
||||
public SearchContentModel(ISearchContentContract.Presenter presenter){
|
||||
mPresenter = presenter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void search(String seek) {
|
||||
NetWork.getSearchApi().search(seek)
|
||||
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<SeachSong>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(SeachSong value) {
|
||||
mPresenter.searchSuccess((ArrayList<SeachSong.DataBean>) value.getData());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
mPresenter.searchError();
|
||||
Log.d(TAG, "onError: "+e.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.example.musicplayer.presenter;
|
||||
|
||||
import com.example.musicplayer.base.BasePresenter;
|
||||
import com.example.musicplayer.contract.ISearchContentContract;
|
||||
import com.example.musicplayer.entiy.SeachSong;
|
||||
import com.example.musicplayer.model.SearchContentModel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by 残渊 on 2018/11/21.
|
||||
*/
|
||||
|
||||
public class SearchContentPresenter extends BasePresenter<ISearchContentContract.View>
|
||||
implements ISearchContentContract.Presenter {
|
||||
private SearchContentModel mModel;
|
||||
|
||||
public SearchContentPresenter(){
|
||||
mModel = new SearchContentModel(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void search(String seek) {
|
||||
mModel.search(seek);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void searchSuccess(ArrayList<SeachSong.DataBean> songListBeans) {
|
||||
if(isAttachView()){
|
||||
getMvpView().setSongsList(songListBeans);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void searchError() {
|
||||
getMvpView().showError();
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package com.example.musicplayer.view;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.media.MediaPlayer;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
|
||||
import com.example.musicplayer.R;
|
||||
import com.example.musicplayer.adapter.SearchContentAdapter;
|
||||
import com.example.musicplayer.contract.ISearchContentContract;
|
||||
import com.example.musicplayer.entiy.SeachSong;
|
||||
import com.example.musicplayer.entiy.Song;
|
||||
import com.example.musicplayer.presenter.SearchContentPresenter;
|
||||
import com.example.musicplayer.util.CommonUtil;
|
||||
import com.example.musicplayer.util.FileHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by 残渊 on 2018/11/21.
|
||||
*/
|
||||
|
||||
public class SearchContentFragment extends Fragment implements ISearchContentContract.View{
|
||||
private static final String TAG="SearchContentFragment";
|
||||
public static final String IS_ONLINE="online";
|
||||
|
||||
private SearchContentPresenter mPresenter;
|
||||
private RecyclerView mRecycler;
|
||||
private LinearLayoutManager manager;
|
||||
private SearchContentAdapter mAdapter;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View view=inflater.inflate(R.layout.fragment_search_content,container,false);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState){
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
mRecycler = getActivity().findViewById(R.id.recycler_song_list);
|
||||
manager = new LinearLayoutManager(getActivity());
|
||||
|
||||
mPresenter = new SearchContentPresenter();
|
||||
mPresenter.attachView(this);
|
||||
mPresenter.search(getSeekContent());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSeekContent() {
|
||||
Log.d(TAG, "getSeekContent: "+getArguments().getString("seek"));
|
||||
return getArguments().getString("seek").trim();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSongsList(final ArrayList<SeachSong.DataBean> songListBeans) {
|
||||
mAdapter = new SearchContentAdapter(songListBeans);
|
||||
mRecycler.setLayoutManager(manager);
|
||||
mRecycler.setAdapter(mAdapter);
|
||||
|
||||
mAdapter.setItemClick(new SearchContentAdapter.ItemClick() {
|
||||
@Override
|
||||
public void onClick(int position) {
|
||||
SeachSong.DataBean dataBean = songListBeans.get(position);
|
||||
Song song= new Song();
|
||||
song.setArtist(dataBean.getSinger());
|
||||
song.setTitle(dataBean.getName());
|
||||
song.setUrl(dataBean.getUrl());
|
||||
song.setImgUrl(dataBean.getPic());
|
||||
song.setCurrent(FileHelper.getSong()==null?0:FileHelper.getSong().getCurrent());
|
||||
FileHelper.saveSong(song);
|
||||
|
||||
Intent intent=new Intent(getActivity(),PlayActivity.class);
|
||||
intent.putExtra(IS_ONLINE,true);
|
||||
startActivity(intent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showError() {
|
||||
CommonUtil.showToast(getActivity(),"连接超时");
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="20dp"
|
||||
android:background="@color/seekColor">
|
||||
</View>
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:background="@color/translucent"
|
||||
android:id="@+id/recycler_song_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginBottom="65dp">
|
||||
|
||||
|
||||
</android.support.v7.widget.RecyclerView>
|
||||
|
||||
|
||||
</LinearLayout>
|
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_toRightOf="@id/iv_playing"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:ellipsize="end"
|
||||
android:maxEms="15"
|
||||
android:singleLine="true"
|
||||
android:textColor="@color/white"
|
||||
android:textSize="18sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_artist"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_marginStart="10dp"
|
||||
android:ellipsize="end"
|
||||
android:maxEms="15"
|
||||
android:singleLine="true"
|
||||
android:textColor="@color/short_white" />
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
<View
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dip"
|
||||
android:background="@color/gray" />
|
||||
</RelativeLayout>
|
Loading…
Reference in new issue