更新了收藏夹功能

接口改成了POST请求
文件路径不再做base64了
This commit is contained in:
yutou 2020-12-03 18:30:39 +08:00
parent 08ec84d1d5
commit 4a08266816
11 changed files with 576 additions and 259 deletions

View File

@ -67,10 +67,8 @@ dependencies {
implementation 'androidx.palette:palette:1.0.0' implementation 'androidx.palette:palette:1.0.0'
implementation 'com.github.zrunker:ZTextView:v1.0.2' implementation 'com.github.zrunker:ZTextView:v1.0.2'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5' // //debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5' //
implementation 'com.kaopiz:kprogresshud:1.2.0' implementation 'com.kaopiz:kprogresshud:1.2.0'
implementation 'com.ogaclejapan.smarttablayout:library:2.0.0@aar'
implementation 'com.ogaclejapan.smarttablayout:utils-v4:2.0.0@aar'
implementation 'com.github.promeg:tinypinyin:2.0.3' implementation 'com.github.promeg:tinypinyin:2.0.3'
implementation 'com.github.myinnos:AlphabetIndex-Fast-Scroll-RecyclerView:1.0.95' implementation 'com.github.myinnos:AlphabetIndex-Fast-Scroll-RecyclerView:1.0.95'
implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

View File

@ -3,6 +3,7 @@ package com.yutou.nas_music_player.Adapters;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.renderscript.AllocationAdapter;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -35,10 +36,16 @@ public class AlbumsRecyclerAdapter extends RecyclerView.Adapter<AlbumsRecyclerAd
List<MusicData> list; List<MusicData> list;
Context context; Context context;
MediaBrowserHelper browserHelper; MediaBrowserHelper browserHelper;
int model;
int fid;
public AlbumsRecyclerAdapter(Context context, List<MusicData> list) { public AlbumsRecyclerAdapter(Context context, List<MusicData> list,int model) {
this.list = list; this.list = list;
this.context = context; this.context = context;
this.model=model;
}
public void setFid(int fid){
this.fid=fid;
} }
@NonNull @NonNull
@ -105,12 +112,20 @@ public class AlbumsRecyclerAdapter extends RecyclerView.Adapter<AlbumsRecyclerAd
} }
}); });
holder.layout.setOnClickListener(new View.OnClickListener() { holder.collection.setOnClickListener(view->{
MusicContainer.getLibs().removeCollection(fid, new NetworkInterface() {
@Override @Override
public void onClick(View view) { public void httpGetData(Object data, int state) {
browserHelper.play(data); AlbumsRecyclerAdapter.this.notifyDataSetChanged();
} }
@Override
public void httpError(Exception e) {
}
}, data);
}); });
holder.layout.setOnClickListener(view -> browserHelper.play(data));
if(MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())){ if(MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())){
holder.play_mask.setVisibility(View.VISIBLE); holder.play_mask.setVisibility(View.VISIBLE);
}else{ }else{

View File

@ -14,6 +14,7 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
@ -49,28 +50,28 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
public static final int LIBS_MODEL_Tmp = 3; public static final int LIBS_MODEL_Tmp = 3;
public static final int LIBS_MODEL_PLAY_LIST = 4; public static final int LIBS_MODEL_PLAY_LIST = 4;
public static final int LIBS_MODEL_PLAY_Collection = 5; public static final int LIBS_MODEL_PLAY_Collection = 5;
private MusicLibsAdapter adapter; KProgressHUD hud;
List<MusicData> list; List<MusicData> list;
Map<Integer, List<MusicData>> listMap;
Map<String, Bitmap> icons; Map<String, Bitmap> icons;
Context context;
public MusicLibsAdapter(Context context, int model) { public MusicLibsAdapter(Context context, int model) {
super(context, R.layout.layout_music_libs_item); super(context, R.layout.layout_music_libs_item);
list = new ArrayList<>(); list = new ArrayList<>();
icons = new HashMap<>(); icons = new HashMap<>();
listMap = new HashMap<>(); this.context = context;
adapter = this; initData(model, true);
initData(model);
} }
private int model = -1; private int model = -1;
private String musicPath = "root"; private String musicPath = "root";
public void initData(int model) { public void initData(int model, boolean init) {
this.model = model; this.model = model;
list.clear(); list.clear();
if (!init)
hud = AppTools.showLoading(context);
System.out.println("填充序列 设置model " + model + " " + this.model + " &"); System.out.println("填充序列 设置model " + model + " " + this.model + " &");
Map<String, List<MusicData>> map; Map<String, List<MusicData>> map;
switch (model) { switch (model) {
@ -78,6 +79,11 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
String album = null; String album = null;
MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() { MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() {
@Override @Override
@ -86,11 +92,14 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
if (data != null && state == 0) if (data != null && state == 0)
list.addAll((List<MusicData>) data); list.addAll((List<MusicData>) data);
notifyDataSetChanged(); notifyDataSetChanged();
if (hud != null)
hud.dismiss();
} }
@Override @Override
public void httpError(Exception e) { public void httpError(Exception e) {
if (hud != null)
hud.dismiss();
} }
}); });
} }
@ -101,6 +110,9 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
break; break;
case LIBS_MODEL_Tmp: case LIBS_MODEL_Tmp:
list.addAll(MusicContainer.getInstance().getTmpPlayList()); list.addAll(MusicContainer.getInstance().getTmpPlayList());
System.out.println("临时播放列表:" + MusicContainer.getInstance().getTmpPlayList().size());
if (hud != null)
hud.dismiss();
break; break;
case LIBS_MODEL_PLAY_LIST: case LIBS_MODEL_PLAY_LIST:
list.addAll(MusicContainer.getInstance().getPlayList()); list.addAll(MusicContainer.getInstance().getPlayList());
@ -109,8 +121,12 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
String collection=null; try {
MusicContainer.getLibs().onCollectionList(collection,new NetworkInterface(){ Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
MusicContainer.getLibs().onCollectionList(null, new NetworkInterface() {
@Override @Override
public void httpGetData(Object data, int state) { public void httpGetData(Object data, int state) {
@ -118,11 +134,14 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
if (data != null && state == 0) if (data != null && state == 0)
list.addAll((List<MusicData>) data); list.addAll((List<MusicData>) data);
notifyDataSetChanged(); notifyDataSetChanged();
if (hud != null)
hud.dismiss();
} }
@Override @Override
public void httpError(Exception e) { public void httpError(Exception e) {
if (hud != null)
hud.dismiss();
} }
}); });
} }
@ -134,6 +153,11 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
MusicContainer.getLibs().onDirMusicData(musicPath, new NetworkInterface() { MusicContainer.getLibs().onDirMusicData(musicPath, new NetworkInterface() {
@Override @Override
public void httpGetData(Object data, int state) { public void httpGetData(Object data, int state) {
@ -144,21 +168,26 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
@Override @Override
public void run() { public void run() {
notifyDataSetChanged(); notifyDataSetChanged();
if (hud != null)
hud.dismiss();
} }
}); });
} }
@Override @Override
public void httpError(Exception e) { public void httpError(Exception e) {
if (hud != null)
hud.dismiss();
} }
}); });
} }
}).start(); }).start();
} }
listMap.put(model, list); }
public void initData(int model) {
initData(model, false);
} }
@Override @Override
@ -198,9 +227,18 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
holder.title.setText(data.getTitle()); holder.title.setText(data.getTitle());
holder.artist.setText(data.getArtist()); holder.artist.setText(data.getArtist());
holder.icon.setImageResource(R.drawable.ic_dir); holder.icon.setImageResource(R.drawable.ic_dir);
if (model == LIBS_MODEL_PLAY_Collection) {
holder.collection.setVisibility(View.GONE);
} else {
holder.collection.setVisibility(View.VISIBLE);
}
if (!data.isDir()) { if (!data.isDir()) {
if (model != LIBS_MODEL_Album && model != LIBS_MODEL_Artist) { if (model != LIBS_MODEL_Album && model != LIBS_MODEL_Artist) {
if (model == LIBS_MODEL_PLAY_Collection) {
holder.message.setText("" + data.getComment() + " 首歌");
} else {
holder.message.setText(data.getBitRate() + "kbps | " + data.getSampleRate() + "hz | " + data.getEncodingType()); holder.message.setText(data.getBitRate() + "kbps | " + data.getSampleRate() + "hz | " + data.getEncodingType());
}
holder.message.setVisibility(View.VISIBLE); holder.message.setVisibility(View.VISIBLE);
holder.artist.setVisibility(View.VISIBLE); holder.artist.setVisibility(View.VISIBLE);
} else {//为专辑或艺术家 } else {//为专辑或艺术家
@ -238,6 +276,45 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
} else { } else {
holder.play_mask.setVisibility(View.GONE); holder.play_mask.setVisibility(View.GONE);
} }
holder.collection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (data.isDir()) {
if (model == LIBS_MODEL_Album) {//添加整个专辑
MusicContainer.getLibs().onAlbumMusicData(data.getAlbum(), new NetworkInterface() {
@Override
public void httpGetData(Object md, int state) {
List<MusicData> list = (List<MusicData>) md;
MusicContainer.getLibs().onAddCollectionDir(context, list.toArray(new MusicData[0]));
}
@Override
public void httpError(Exception e) {
}
});
} else {//添加整个文件夹
JSONObject json = new JSONObject();
json.put("path", data.getFileBase64());
json.put("type", true);
MusicContainer.getLibs().onDirMusicData(json, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
List<MusicData> list = (List<MusicData>) data;
MusicContainer.getLibs().onAddCollectionDir(context, list.toArray(new MusicData[0]));
}
@Override
public void httpError(Exception e) {
}
});
}
} else {//单曲
MusicContainer.getLibs().onAddCollectionDir(context, data);
}
}
});
holder.top.setOnClickListener(new View.OnClickListener() { holder.top.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@ -257,7 +334,10 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
private void addAlbumList(String album) { private void addAlbumList(String album) {
System.out.println("添加专辑:" + album); System.out.println("添加专辑:" + album);
List<MusicData> list = MusicContainer.getInstance().getAllAlbumMapList().get(album); MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
List<MusicData> list = (List<MusicData>) data;
for (MusicData musicData : list) { for (MusicData musicData : list) {
System.out.println(musicData.getTitle() + "| 专辑名 =" + musicData.getAlbum()); System.out.println(musicData.getTitle() + "| 专辑名 =" + musicData.getAlbum());
MusicContainer.getInstance().addTmpList(musicData); MusicContainer.getInstance().addTmpList(musicData);
@ -265,6 +345,13 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
Toast.makeText(getContext(), "已添加到队列中", Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), "已添加到队列中", Toast.LENGTH_LONG).show();
} }
@Override
public void httpError(Exception e) {
}
});
}
private void showAddTmpListDialog(MusicData data) { private void showAddTmpListDialog(MusicData data) {
AlertDialog dialog = new AlertDialog.Builder(getContext()) AlertDialog dialog = new AlertDialog.Builder(getContext())
.setTitle("添加文件夹到队列") .setTitle("添加文件夹到队列")
@ -331,13 +418,6 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
for (String integer : icons.keySet()) { for (String integer : icons.keySet()) {
Objects.requireNonNull(icons.get(integer)).recycle(); Objects.requireNonNull(icons.get(integer)).recycle();
} }
for (Integer integer : listMap.keySet()) {
for (MusicData data : Objects.requireNonNull(listMap.get(integer))) {
data.recycleImg();
}
Objects.requireNonNull(listMap.get(integer)).clear();
}
listMap.clear();
icons.clear(); icons.clear();
} }
@ -354,7 +434,6 @@ public class MusicLibsAdapter extends ArrayAdapter<MusicData> {
} }
public static class ViewHolder extends RecyclerView.ViewHolder { public static class ViewHolder extends RecyclerView.ViewHolder {
MarqueeTextView title, artist, message; MarqueeTextView title, artist, message;
LinearLayout buttons; LinearLayout buttons;

View File

@ -195,7 +195,7 @@ public class MusicData {
public String getFileBase64() { public String getFileBase64() {
if (StringUtil.isEmpty(file)) if (StringUtil.isEmpty(file))
return null; return null;
return new String(Base64.encode(file.getBytes(), Base64.DEFAULT)).replace("\n", ""); return file;
} }
public String getPlayUrl() { public String getPlayUrl() {
@ -388,7 +388,8 @@ public class MusicData {
@Override @Override
public String toString() { public String toString() {
return "MusicData{" + return "MusicData{" +
"artist='" + artist + '\'' + "id=" + id +
", artist='" + artist + '\'' +
", album='" + album + '\'' + ", album='" + album + '\'' +
", title='" + title + '\'' + ", title='" + title + '\'' +
", comment='" + comment + '\'' + ", comment='" + comment + '\'' +
@ -410,7 +411,6 @@ public class MusicData {
", lossless=" + lossless + ", lossless=" + lossless +
", variableBitRate=" + variableBitRate + ", variableBitRate=" + variableBitRate +
", md5='" + md5 + '\'' + ", md5='" + md5 + '\'' +
", isDownloadImg=" + isDownloadImg +
'}'; '}';
} }
} }

View File

@ -1,18 +1,25 @@
package com.yutou.nas_music_player.containers; package com.yutou.nas_music_player.containers;
import android.content.Context;
import android.content.DialogInterface;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.os.Build; import android.os.Build;
import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.MediaMetadataCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Datas.PreviousPlayerList; import com.yutou.nas_music_player.Datas.PreviousPlayerList;
import com.yutou.nas_music_player.Interfaces.DownloadInterface;
import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.MyApplication; import com.yutou.nas_music_player.MyApplication;
import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.tools.CollectionTools; import com.yutou.nas_music_player.tools.CollectionTools;
import com.yutou.nas_music_player.tools.ConfigTools; import com.yutou.nas_music_player.tools.ConfigTools;
import com.yutou.nas_music_player.tools.NetworkTool; import com.yutou.nas_music_player.tools.NetworkTool;
@ -21,9 +28,7 @@ import com.yutou.nas_music_player.tools.StringUtil;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -457,12 +462,158 @@ public class MusicContainer {
initData(); initData();
} }
public void onCollectionList(String collection, NetworkInterface networkInterface) { public void onAddCollection(String md5, int fid) {
JSONObject json=new JSONObject();
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_List, json, new NetworkInterface() { }
public void onAddCollectionDir(Context context, MusicData... collectionData) {
onCollectionList(null, new NetworkInterface() {
@Override @Override
public void httpGetData(Object data, int state) { public void httpGetData(Object data, int state) {
List<MusicData> list = (List<MusicData>) data;
List<String> titles = new ArrayList<>();
titles.add("添加新收藏夹");
for (MusicData musicData : list) {
titles.add(musicData.getTitle());
}
AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle("添加到收藏夹")
.setItems((CharSequence[]) titles.toArray(new CharSequence[list.size()]), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {//添加新收藏夹
EditText editText = new EditText(context);
new AlertDialog.Builder(context)
.setTitle("收藏夹名字")
.setView(editText)
.setPositiveButton("提交", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (StringUtil.isEmpty(editText.getText().toString())) {
Toast.makeText(context, "名字不能为空", Toast.LENGTH_LONG).show();
return;
}
JSONObject json = new JSONObject();
json.put("favorite", editText.getText().toString());
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_Dir_Add, json, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
JSONObject json = JSONObject.parseObject((String) data);
Toast.makeText(context, json.getString("msg"), Toast.LENGTH_LONG).show();
dialog.dismiss();
}
@Override
public void httpError(Exception e) {
dialog.dismiss();
}
});
}
})
.setNegativeButton("取消", (dialog1, which1) -> dialog1.dismiss()).create().show();
} else {//选择现有收藏夹
MusicData musicData = list.get(which - 1);
JSONArray array = new JSONArray();
for (MusicData data : collectionData) {
if (!StringUtil.isEmpty(data.getMd5()))
array.add(data.getMd5());
}
JSONObject json = new JSONObject();
json.put("fid", musicData.getId());
json.put("md5s", array);
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_Add, json, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
JSONObject json = JSONObject.parseObject((String) data);
Toast.makeText(context, json.getString("msg"), Toast.LENGTH_LONG).show();
dialog.dismiss();
}
@Override
public void httpError(Exception e) {
dialog.dismiss();
}
});
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
dialog.show();
}
@Override
public void httpError(Exception e) {
}
});
}
public void onCollectionList(String collection, NetworkInterface networkInterface) {
JSONObject json = new JSONObject();
String url = NetworkTool.NetworkAPI.MUSIC_Collection_Dir_List;
if (collection != null) {
url = NetworkTool.NetworkAPI.MUSIC_Collection_Get;
json.put("fid",Integer.parseInt(collection));
}
NetworkTool.init().httpGet(url, json, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
JSONObject json = JSONObject.parseObject((String) data);
if (json.getInteger("code") == 0) {
if(json.getJSONArray("data").toJSONString().contains("md5")){
networkInterface.httpGetData(JSONArray.parseArray(json.getJSONArray("data").toJSONString(), MusicData.class), 0);
}else{
List<MusicData> list = new ArrayList<>();
for (Object album : json.getJSONArray("data")) {
JSONObject js = (JSONObject) album;
MusicData musicData = new MusicData();
musicData.setId(js.getInteger("id"));
musicData.setComment(js.getLong("count") + "");
musicData.setTitle(js.getString("title"));
musicData.setAlbum(js.getString("title"));
musicData.setDir(false);
list.add(musicData);
}
networkInterface.httpGetData(list, 0);
}
} else {
networkInterface.httpGetData(new ArrayList<>(), 0);
}
}
@Override
public void httpError(Exception e) {
}
});
}
public void removeCollection(int fid,NetworkInterface networkInterface,MusicData... musicData){
JSONObject json=new JSONObject();
JSONArray array=new JSONArray();
for (MusicData data : musicData) {
JSONObject js=new JSONObject();
js.put("md5",data.getMd5());
js.put("fid",fid);
array.add(js);
}
json.put("data",array);
NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_Remove, json, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
JSONObject json=JSONObject.parseObject((String) data);
if(networkInterface!=null){
networkInterface.httpGetData(data,state);
}
Toast.makeText(MyApplication.application,json.getString("msg"),Toast.LENGTH_LONG).show();
} }
@Override @Override
@ -480,32 +631,6 @@ public class MusicContainer {
if (initInterface != null) if (initInterface != null)
initInterface.init(); initInterface.init();
initInterface = null; initInterface = null;
/*NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_ALL, new JSONObject(), new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
JSONObject json = JSONObject.parseObject(data.toString());
if (json.getInteger("code") == 0) {
mainData = JSONArray.parseArray(json.getJSONArray("data").toJSONString(), MusicData.class);
System.out.println(mainData.size());
if (mainData.size() > 0) {
nowPlayData = mainData.get(0);
}
System.out.println("获取列表完成");
for (MusicData mainDatum : mainData) {
if (StringUtil.isEmpty(mainDatum.getMd5())) {
System.out.println(mainDatum.getFile());
}
}
updateLibsMap();
}
}
@Override
public void httpError(Exception e) {
}
});*/
} }
public void onAlbumMusicData(String album, NetworkInterface networkInterface) { public void onAlbumMusicData(String album, NetworkInterface networkInterface) {

View File

@ -18,15 +18,19 @@ import java.security.MessageDigest;
public class AppTools { public class AppTools {
public static KProgressHUD showLoading(Context context){ public static KProgressHUD showLoading(Context context,String title){
return KProgressHUD.create(context) return KProgressHUD.create(context)
.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE) .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
.setDetailsLabel("loading...") .setDetailsLabel(title)
.setCancellable(true) .setCancellable(true)
.setAnimationSpeed(2) .setAnimationSpeed(2)
.setDimAmount(0.5f) .setDimAmount(0.5f)
.show(); .show();
} }
public static KProgressHUD showLoading(Context context){
return showLoading(context,"loading...");
}
public static void saveBitmap(final Bitmap bitmap, final String saveKey){ public static void saveBitmap(final Bitmap bitmap, final String saveKey){
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override

View File

@ -37,7 +37,12 @@ public class NetworkTool {
public static String HOME = ""; public static String HOME = "";
public static String MUSIC_LIST = HOME + "/music/list.do"; public static String MUSIC_LIST = HOME + "/music/list.do";
public static String MUSIC_Album = HOME + "/music/getAlbum.do"; public static String MUSIC_Album = HOME + "/music/getAlbum.do";
public static String MUSIC_Collection_List = HOME + "/music/favorite/dir/list.do"; public static String MUSIC_Collection_Dir_List = HOME + "/music/favorite/dir/list.do";
public static String MUSIC_Collection_Dir_Add = HOME + "/music/favorite/dir/add.do";
public static String MUSIC_Collection_Dir_ReName = HOME + "/music/favorite/dir/rename.do";
public static String MUSIC_Collection_Get = HOME + "/music/favorite/get.do";
public static String MUSIC_Collection_Add = HOME + "/music/favorite/add.do";
public static String MUSIC_Collection_Remove = HOME + "/music/favorite/remove.do";
public static String MUSIC_ALL = HOME + "/music/all.do"; public static String MUSIC_ALL = HOME + "/music/all.do";
public static String MUSIC_METADATA = HOME + "/music/find/file.do"; public static String MUSIC_METADATA = HOME + "/music/find/file.do";
public static String MUSIC_IMAGE = HOME + "/music/image.do"; public static String MUSIC_IMAGE = HOME + "/music/image.do";
@ -53,7 +58,8 @@ public class NetworkTool {
public static String donwloadPath = AppData.dir + AppData.download_dir + File.separator; public static String donwloadPath = AppData.dir + AppData.download_dir + File.separator;
private NetworkTool() { private NetworkTool() {
HOME = "http://192.168.31.92:8000/nas"; //HOME = "http://192.168.31.92:8000/nas";
HOME = "http://192.168.137.1:8000/nas";
if (StringUtil.isEmpty(HOME)) { if (StringUtil.isEmpty(HOME)) {
httpGet("http://tools.yutou233.cn/nas/music/getlocalhost.do", new JSONObject(), new NetworkInterface() { httpGet("http://tools.yutou233.cn/nas/music/getlocalhost.do", new JSONObject(), new NetworkInterface() {
@Override @Override
@ -83,7 +89,11 @@ public class NetworkTool {
return network; return network;
} }
public void httpGet(final String url, final JSONObject body, final NetworkInterface networkInterface) { public void httpGet(String url, JSONObject body, NetworkInterface networkInterface) {
httpPost(url, body, networkInterface);
}
@Deprecated
public void httpGet_old(final String url, final JSONObject body, final NetworkInterface networkInterface) {
if (!url.startsWith("http:") && !StringUtil.isEmpty(HOME)) { if (!url.startsWith("http:") && !StringUtil.isEmpty(HOME)) {
httpGet(HOME + url, body, networkInterface); httpGet(HOME + url, body, networkInterface);
return; return;
@ -148,6 +158,19 @@ public class NetworkTool {
private Map<JSONObject, Integer> map = Collections.synchronizedMap(new HashMap<JSONObject, Integer>()); private Map<JSONObject, Integer> map = Collections.synchronizedMap(new HashMap<JSONObject, Integer>());
public void httpPost(final String url, final JSONObject body, final NetworkInterface networkInterface) { public void httpPost(final String url, final JSONObject body, final NetworkInterface networkInterface) {
if (!url.startsWith("http:") && !StringUtil.isEmpty(HOME)) {
httpPost(HOME + url, body, networkInterface);
return;
}
if (StringUtil.isEmpty(HOME) && !url.contains("getlocalhost.do")) {
AppData.handler.postDelayed(new Runnable() {
@Override
public void run() {
NetworkTool.init().httpPost(url, body, networkInterface);
}
}, 300);
return;
}
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
@ -161,14 +184,16 @@ public class NetworkTool {
connection.setDoInput(true); connection.setDoInput(true);
connection.setConnectTimeout(5 * 1000); connection.setConnectTimeout(5 * 1000);
connection.setReadTimeout(10 * 1000); connection.setReadTimeout(10 * 1000);
connection.addRequestProperty("Connection", "keep-alive"); //connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("User-Agent", getExtUa()); //connection.addRequestProperty("User-Agent", getExtUa());
connection.addRequestProperty("content-type","application/json");
connection.addRequestProperty("charset","UTF-8");
OutputStream outputStream = connection.getOutputStream(); OutputStream outputStream = connection.getOutputStream();
String bytes = body.toString(); outputStream.write(body.toJSONString().getBytes());
outputStream.write(bytes.getBytes());
outputStream.flush(); outputStream.flush();
outputStream.close(); outputStream.close();
System.out.println(connection.getResponseCode());
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((tmp = reader.readLine()) != null) { while ((tmp = reader.readLine()) != null) {
str.append(tmp); str.append(tmp);
@ -180,12 +205,22 @@ public class NetworkTool {
if (map.containsKey(body)) { if (map.containsKey(body)) {
map.remove(body); map.remove(body);
} }
if (networkInterface != null) AppData.handler.post(new Runnable() {
@Override
public void run() {
if (networkInterface != null) {
try {
networkInterface.httpGetData(str.toString(), connection.getResponseCode()); networkInterface.httpGetData(str.toString(), connection.getResponseCode());
connection.disconnect(); } catch (IOException e) {
} catch (SocketTimeoutException e) { e.printStackTrace();
Log.e(TAG, url + "\n传参:" + body.toString() + "\n密文" + str + "\n" + e); }
}
}
});
connection.disconnect();
reader.close();
} catch (SocketTimeoutException e) {
synchronized (NetworkTool.this) { synchronized (NetworkTool.this) {
if (!map.containsKey(body)) { if (!map.containsKey(body)) {
@ -203,11 +238,14 @@ public class NetworkTool {
} catch (ConnectException e) { } catch (ConnectException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
if (networkInterface != null) if (networkInterface != null)
networkInterface.httpError(e); networkInterface.httpError(e);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
networkInterface.httpError(e); networkInterface.httpError(e);
}finally {
Log.e(TAG, url + "\n传参:" + body.toString() + "\n接收" + str );
} }
} }
}).start(); }).start();

View File

@ -23,6 +23,7 @@ import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.R; import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer; import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.tools.StringUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -34,22 +35,67 @@ public class AlbumsActivity extends AppCompatActivity {
private RecyclerView listView; private RecyclerView listView;
private MediaBrowserHelper browserHelper; private MediaBrowserHelper browserHelper;
private AlbumsRecyclerAdapter adapter; private AlbumsRecyclerAdapter adapter;
private int model;//0 专辑, 1 歌手, 2 收藏
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_album); setContentView(R.layout.activity_album);
String album=getIntent().getStringExtra("album"); model = getIntent().getIntExtra("model", -1);
initView(); initView();
initData(album); switch (model) {
case 2:
initDataOfCollection(getIntent().getIntExtra("fid", -1));
break;
case 0:
initData(getIntent().getStringExtra("album"));
break;
default:
finish();
}
browserHelper = new MediaBrowserHelper(this); browserHelper = new MediaBrowserHelper(this);
browserHelper.regPlayListener(new PlayListener()); browserHelper.regPlayListener(new PlayListener());
adapter.setPlayListener(browserHelper); adapter.setPlayListener(browserHelper);
} }
private static class PlayListener extends MediaControllerCompat.Callback { private static class PlayListener extends MediaControllerCompat.Callback {
} }
private void initDataOfCollection(int fid) {
adapter.setFid(fid);
MusicContainer.getLibs().onCollectionList(fid + "", new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
List<MusicData> list = (List<MusicData>) data;
adapter.setData(list);
adapter.notifyDataSetChanged();
if(list.isEmpty())
return;
list.get(0).getImg(-1, -1, new NetworkInterface() {
@Override
public void httpGetData(Object data, int state) {
album_image.setImageBitmap((Bitmap) data);
Glide.with(AlbumsActivity.this).load(data)
.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3)))
.into(image_background);
listView.setBackgroundColor(list.get(0).getImageColor());
}
@Override
public void httpError(Exception e) {
}
});
}
@Override
public void httpError(Exception e) {
}
});
}
private void initData(String album) { private void initData(String album) {
MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() { MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() {
@Override @Override
@ -88,7 +134,7 @@ public class AlbumsActivity extends AppCompatActivity {
image_background = findViewById(R.id.image_background); image_background = findViewById(R.id.image_background);
listView = findViewById(R.id.album_list); listView = findViewById(R.id.album_list);
listView.setLayoutManager(new LinearLayoutManager(this)); listView.setLayoutManager(new LinearLayoutManager(this));
adapter=new AlbumsRecyclerAdapter(AlbumsActivity.this,new ArrayList<>()); adapter = new AlbumsRecyclerAdapter(AlbumsActivity.this, new ArrayList<>(),model);
listView.setAdapter(adapter); listView.setAdapter(adapter);
} }

View File

@ -3,42 +3,35 @@ package com.yutou.nas_music_player.views.Fragments;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AbsListView; import android.widget.AbsListView;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem;
import com.yutou.nas_music_player.Adapters.MusicLibsAdapter;
import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.views.AlbumsActivity;
import com.yutou.nas_music_player.views.MainActivity;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.core.app.ActivityOptionsCompat; import androidx.core.app.ActivityOptionsCompat;
import androidx.core.util.Pair; import androidx.core.util.Pair;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import java.util.HashMap; import com.yutou.nas_music_player.Adapters.MusicLibsAdapter;
import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.tools.StringUtil;
import com.yutou.nas_music_player.views.AlbumsActivity;
import java.util.List; import java.util.List;
import java.util.Map;
import in.myinnos.alphabetsindexfastscrollrecycler.IndexFastScrollRecyclerView;
public class MusicLibsFragment extends Fragment { public class MusicLibsFragment extends Fragment {
public static Map<Integer, MusicLibsFragment> map = new HashMap<>();
private Context context; private Context context;
private View view; private View view;
private ListView recyclerView; private ListView listView;
private int model = 0; private int model = 0;
private MediaBrowserHelper browserHelper; private MediaBrowserHelper browserHelper;
@ -75,13 +68,7 @@ public class MusicLibsFragment extends Fragment {
if (view != null) if (view != null)
return view; return view;
view = inflater.inflate(R.layout.fragment_music_libs, null); view = inflater.inflate(R.layout.fragment_music_libs, null);
int id = FragmentPagerItem.getPosition(getArguments()); if (adapter == null || listView == null)
if (map == null)
map = new HashMap<>();
if (!map.containsKey(id)) {
map.put(id, this);
}
if (adapter == null || recyclerView == null)
initViews(); initViews();
return view; return view;
} }
@ -89,11 +76,11 @@ public class MusicLibsFragment extends Fragment {
private MusicLibsAdapter adapter; private MusicLibsAdapter adapter;
public void initViews() { public void initViews() {
recyclerView = view.findViewById(R.id.listView); listView = view.findViewById(R.id.listView);
// recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); // recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new MusicLibsAdapter(getContext(), model); adapter = new MusicLibsAdapter(getContext(), model);
recyclerView.setAdapter(adapter); listView.setAdapter(adapter);
recyclerView.setOnScrollListener(new AbsListView.OnScrollListener() { listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override @Override
public void onScrollStateChanged(AbsListView view, int scrollState) { public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
@ -109,7 +96,7 @@ public class MusicLibsFragment extends Fragment {
} }
}); });
recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() { listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (adapter.getModel()) { switch (adapter.getModel()) {
@ -119,8 +106,29 @@ public class MusicLibsFragment extends Fragment {
case MusicLibsAdapter.LIBS_MODEL_Album: case MusicLibsAdapter.LIBS_MODEL_Album:
clickAlbum(position, view); clickAlbum(position, view);
break; break;
case MusicLibsAdapter.LIBS_MODEL_PLAY_Collection:
clickCollection(position, view);
break;
} }
} }
private void clickCollection(int position, View view) {
MusicLibsAdapter.ViewHolder holder = (MusicLibsAdapter.ViewHolder) view.getTag();
Pair<View, String> pImage = Pair.create(holder.itemView, "album_image");
Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), pImage).toBundle();
MusicData data = adapter.getItem(position);
if (data == null)
return;
if(StringUtil.isEmpty(data.getComment())){
Toast.makeText(getContext(),"未收藏任何歌曲",Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(getContext(), AlbumsActivity.class);
intent.putExtra("model",2);
intent.putExtra("fid", data.getId());
startActivity(intent, bundle);
}
private void clickAlbum(int position, View view) { private void clickAlbum(int position, View view) {
MusicLibsAdapter.ViewHolder holder = (MusicLibsAdapter.ViewHolder) view.getTag(); MusicLibsAdapter.ViewHolder holder = (MusicLibsAdapter.ViewHolder) view.getTag();
Pair<View, String> pImage = Pair.create(holder.itemView, "album_image"); Pair<View, String> pImage = Pair.create(holder.itemView, "album_image");
@ -130,9 +138,11 @@ public class MusicLibsFragment extends Fragment {
if (data == null) if (data == null)
return; return;
Intent intent = new Intent(getContext(), AlbumsActivity.class); Intent intent = new Intent(getContext(), AlbumsActivity.class);
intent.putExtra("model",0);
intent.putExtra("album", data.getAlbum()); intent.putExtra("album", data.getAlbum());
startActivity(intent, bundle); startActivity(intent, bundle);
} }
private void clickAllList(int position) { private void clickAllList(int position) {
MusicData musicData = adapter.getItem(position); MusicData musicData = adapter.getItem(position);
if (musicData != null) { if (musicData != null) {
@ -157,7 +167,7 @@ public class MusicLibsFragment extends Fragment {
break; break;
} }
if (pos != -1) if (pos != -1)
recyclerView.setSelection(pos); listView.setSelection(pos);
} }
public MusicLibsAdapter getAdapter() { public MusicLibsAdapter getAdapter() {
@ -181,9 +191,19 @@ public class MusicLibsFragment extends Fragment {
}); });
} }
@Override public void flash() {
public void onDestroyView() { if (adapter == null)
super.onDestroyView(); return;
map.clear(); if (model == MusicLibsAdapter.LIBS_MODEL_Tmp) {
adapter.initData(model);
}
adapter.notifyDataSetChanged();
}
public void update() {
if (adapter == null)
return;
adapter.initData(model);
adapter.notifyDataSetChanged();
} }
} }

View File

@ -8,34 +8,33 @@ import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat; import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat; import android.support.v4.media.session.PlaybackStateCompat;
import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import com.ogaclejapan.smarttablayout.SmartTabLayout; import com.google.android.material.tabs.TabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter; import com.google.android.material.tabs.TabLayoutMediator;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.yutou.nas_music_player.Adapters.MusicLibsAdapter; import com.yutou.nas_music_player.Adapters.MusicLibsAdapter;
import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Datas.MusicData;
import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.Interfaces.NetworkInterface;
import com.yutou.nas_music_player.R; import com.yutou.nas_music_player.R;
import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MediaBrowserHelper;
import com.yutou.nas_music_player.containers.MusicContainer; import com.yutou.nas_music_player.containers.MusicContainer;
import com.yutou.nas_music_player.tools.AppData;
import com.yutou.nas_music_player.views.Fragments.MusicLibsFragment; import com.yutou.nas_music_player.views.Fragments.MusicLibsFragment;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import cc.ibooker.ztextviewlib.MarqueeTextView; import cc.ibooker.ztextviewlib.MarqueeTextView;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
@ -49,8 +48,8 @@ public class PlayLibsActivity extends AppCompatActivity {
private Handler handler; private Handler handler;
private int bar_pos = 0; private int bar_pos = 0;
private SmartTabLayout tabLayout; private TabLayout tabLayout;
private ViewPager viewPager; private ViewPager2 viewPager;
private int index = 0; private int index = 0;
@Override @Override
@ -108,6 +107,7 @@ public class PlayLibsActivity extends AppCompatActivity {
}); });
} }
List<MusicLibsFragment> fragments;
private void initView() { private void initView() {
play = findViewById(R.id.collection); play = findViewById(R.id.collection);
next = findViewById(R.id.top); next = findViewById(R.id.top);
@ -116,61 +116,63 @@ public class PlayLibsActivity extends AppCompatActivity {
title = findViewById(R.id.title); title = findViewById(R.id.title);
artist = findViewById(R.id.artist); artist = findViewById(R.id.artist);
progressBar = findViewById(R.id.progressBar); progressBar = findViewById(R.id.progressBar);
tabLayout = findViewById(R.id.viewpagertab); tabLayout = findViewById(R.id.tabLayout);
viewPager = findViewById(R.id.viewpager); viewPager = findViewById(R.id.viewPager);
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter( fragments=new ArrayList<>();
getSupportFragmentManager(), FragmentPagerItems.with(this) fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_ALL));
.add("全部", MusicLibsFragment.class) fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_Album));
.add("专辑", MusicLibsFragment.class) fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_PLAY_Collection));
.add("收藏夹", MusicLibsFragment.class) fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_Tmp));
.add("队列", MusicLibsFragment.class) viewPager.setAdapter(new FragmentStateAdapter(this) {
.create()); @NonNull
viewPager.setAdapter(adapter);
tabLayout.setViewPager(viewPager);
tabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
boolean init = false;
@Override @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { public Fragment createFragment(int position) {
if (!init) { return fragments.get(position);
MusicLibsFragment fragment = MusicLibsFragment.map.get(position);
if (fragment != null) {
fragment.setPlayContainer(browserHelper);
init = true;
}
}
} }
@Override @Override
public void onPageSelected(int position) { public int getItemCount() {
System.out.println("debug 切换页面:" + position); return fragments.size();
index = position;
MusicLibsFragment fragment = MusicLibsFragment.map.get(position);
if (fragment != null) {
fragment.setPlayContainer(browserHelper);
switch (position) {
case 0:
fragment.setModel(MusicLibsAdapter.LIBS_MODEL_ALL);
break;
case 1:
fragment.setModel(MusicLibsAdapter.LIBS_MODEL_Album);
break;
case 2:
fragment.setModel(MusicLibsAdapter.LIBS_MODEL_PLAY_Collection);
break;
case 3:
fragment.setModel(MusicLibsAdapter.LIBS_MODEL_Tmp);
break;
}
} else {
System.out.println("debug 切换页面 没得页面");
}
}
@Override
public void onPageScrollStateChanged(int state) {
} }
}); });
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
fragments.get(tab.getPosition()).flash();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
fragments.get(tab.getPosition()).update();
}
});
TabLayoutMediator mediator=new TabLayoutMediator(tabLayout, viewPager, true, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
switch (position){
case 0:
tab.setText("全部");
break;
case 1:
tab.setText("专辑");
break;
case 2:
tab.setText("收藏");
break;
case 3:
tab.setText("队列");
break;
}
}
});
mediator.attach();
} }
private void setImage(Bitmap data) { private void setImage(Bitmap data) {
@ -198,9 +200,6 @@ public class PlayLibsActivity extends AppCompatActivity {
} }
}); });
if (MusicLibsFragment.map != null && MusicLibsFragment.map.containsKey(index))
MusicLibsFragment.map.get(index).getAdapter().notifyDataSetChanged();
} }
@Override @Override
@ -208,11 +207,6 @@ public class PlayLibsActivity extends AppCompatActivity {
super.onDestroy(); super.onDestroy();
browserHelper.onStop(); browserHelper.onStop();
browserHelper = null; browserHelper = null;
for (Integer integer : MusicLibsFragment.map.keySet()) {
MusicLibsFragment.map.get(integer).onDestroy();
}
MusicLibsFragment.map.clear();
MusicLibsFragment.map = null;
finish(); finish();
} }

View File

@ -14,46 +14,44 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<com.ogaclejapan.smarttablayout.SmartTabLayout <com.google.android.material.tabs.TabLayout
android:id="@+id/viewpagertab" android:id="@+id/tabLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="48dp" android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include" android:background="#F3F5F5"
app:stl_clickable="true" app:layout_constraintTop_toBottomOf="@+id/include">
app:stl_defaultTabBackground="?attr/selectableItemBackground"
app:stl_defaultTabTextAllCaps="true"
app:stl_defaultTabTextColor="#FC000000"
app:stl_defaultTabTextHorizontalPadding="16dp"
app:stl_defaultTabTextMinWidth="0dp"
app:stl_defaultTabTextSize="12sp"
app:stl_distributeEvenly="false"
app:stl_dividerColor="#4D000000"
app:stl_dividerThickness="1dp"
app:stl_drawDecorationAfterTab="false"
app:stl_indicatorAlwaysInCenter="false"
app:stl_indicatorColor="#40C4FF"
app:stl_indicatorCornerRadius="2dp"
app:stl_indicatorGravity="bottom"
app:stl_indicatorInFront="false"
app:stl_indicatorInterpolation="smart"
app:stl_indicatorThickness="4dp"
app:stl_indicatorWidth="auto"
app:stl_indicatorWithoutPadding="false"
app:stl_overlineColor="#4D000000"
app:stl_overlineThickness="0dp"
app:stl_titleOffset="24dp"
app:stl_underlineColor="#4D000000"
app:stl_underlineThickness="1dp" />
<androidx.viewpager.widget.ViewPager <com.google.android.material.tabs.TabItem
android:id="@+id/viewpager" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全部" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="专辑" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收藏" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="队列" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_below="@id/viewpagertab"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewpagertab" /> app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>