|
|
|
@ -17,34 +17,53 @@ import com.example.musicplayer.util.FileUtil;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by 残渊 on 2018/11/27.
|
|
|
|
|
* AlbumSongAdapter 是一个继承自 RecyclerView.Adapter 的适配器类,用于展示专辑中的歌曲列表。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class AlbumSongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
|
|
|
|
|
|
|
|
// 存储歌曲数据的列表
|
|
|
|
|
private List<AlbumSong.DataBean.ListBean> mSongsBeanList;
|
|
|
|
|
// 记录上一次点击的位置
|
|
|
|
|
private int mLastPosition = -1;
|
|
|
|
|
// 点击事件的回调接口
|
|
|
|
|
private OnItemClickListener mSongClick;
|
|
|
|
|
// 歌曲类型的视图类型
|
|
|
|
|
private final int songType = 1;
|
|
|
|
|
// 底部视图的类型
|
|
|
|
|
private final int footerType = 2;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,初始化歌曲列表。
|
|
|
|
|
* @param songsBeans 歌曲数据列表
|
|
|
|
|
*/
|
|
|
|
|
public AlbumSongAdapter(List<AlbumSong.DataBean.ListBean> songsBeans) {
|
|
|
|
|
mSongsBeanList = songsBeans;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置歌曲点击事件的回调接口。
|
|
|
|
|
* @param songClick 点击事件回调
|
|
|
|
|
*/
|
|
|
|
|
public void setSongClick(OnItemClickListener songClick) {
|
|
|
|
|
mSongClick = songClick;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建新的ViewHolder实例。
|
|
|
|
|
* @param parent 父布局
|
|
|
|
|
* @param viewType 视图类型
|
|
|
|
|
* @return 返回对应的ViewHolder
|
|
|
|
|
*/
|
|
|
|
|
@NonNull
|
|
|
|
|
@Override
|
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
|
// 根据视图类型创建不同的ViewHolder
|
|
|
|
|
if (viewType == songType) {
|
|
|
|
|
View view = LayoutInflater.from(parent.getContext())
|
|
|
|
|
.inflate(R.layout.recycler_song_search_item, parent, false);
|
|
|
|
|
ViewHolder viewHolder = new ViewHolder(view);
|
|
|
|
|
return viewHolder;
|
|
|
|
|
}else{
|
|
|
|
|
} else {
|
|
|
|
|
View view = LayoutInflater.from(parent.getContext())
|
|
|
|
|
.inflate(R.layout.footer_view_player_height, parent, false);
|
|
|
|
|
FooterHolder footerHolder = new FooterHolder(view);
|
|
|
|
@ -52,12 +71,18 @@ public class AlbumSongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将数据绑定到ViewHolder上。
|
|
|
|
|
* @param viewHolder 当前ViewHolder
|
|
|
|
|
* @param position 当前位置
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {
|
|
|
|
|
// 如果是歌曲类型的ViewHolder
|
|
|
|
|
if (viewHolder instanceof ViewHolder) {
|
|
|
|
|
ViewHolder holder = (ViewHolder) viewHolder;
|
|
|
|
|
AlbumSong.DataBean.ListBean songsBean = mSongsBeanList.get(position);
|
|
|
|
|
//设置歌手,因为歌手可能有两个
|
|
|
|
|
// 设置歌手名称,可能包含多个歌手
|
|
|
|
|
StringBuilder singer = new StringBuilder(songsBean.getSinger().get(0).getName());
|
|
|
|
|
for (int i = 1; i < songsBean.getSinger().size(); i++) {
|
|
|
|
|
singer.append("、").append(songsBean.getSinger().get(i).getName());
|
|
|
|
@ -65,8 +90,8 @@ public class AlbumSongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|
|
|
|
holder.artistTv.setText(singer.toString());
|
|
|
|
|
holder.titleTv.setText(songsBean.getSongname());
|
|
|
|
|
holder.mItemView.setBackgroundResource(R.color.translucent);
|
|
|
|
|
//根据点击显示
|
|
|
|
|
if(songsBean.getSongmid().equals(FileUtil.getSong().getSongId())){
|
|
|
|
|
// 根据当前播放的歌曲设置不同的显示效果
|
|
|
|
|
if(songsBean.getSongmid().equals(FileUtil.getSong().getSongId())){
|
|
|
|
|
holder.playLine.setVisibility(View.VISIBLE);
|
|
|
|
|
holder.titleTv.setTextColor(App.getContext().getResources().getColor(R.color.yellow));
|
|
|
|
|
holder.artistTv.setTextColor(App.getContext().getResources().getColor(R.color.yellow));
|
|
|
|
@ -76,6 +101,7 @@ public class AlbumSongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|
|
|
|
holder.titleTv.setTextColor(App.getContext().getResources().getColor(R.color.white));
|
|
|
|
|
holder.artistTv.setTextColor(App.getContext().getResources().getColor(R.color.white_blue));
|
|
|
|
|
}
|
|
|
|
|
// 设置点击事件
|
|
|
|
|
holder.mItemView.setOnRippleCompleteListener(rippleView -> {
|
|
|
|
|
mSongClick.onClick(position);
|
|
|
|
|
equalPosition(position);
|
|
|
|
@ -83,16 +109,28 @@ public class AlbumSongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回总的条目数,包括底部视图。
|
|
|
|
|
* @return 总条目数
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public int getItemCount() {
|
|
|
|
|
return mSongsBeanList.size() + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据位置返回不同的视图类型。
|
|
|
|
|
* @param position 位置
|
|
|
|
|
* @return 视图类型
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
|
return position + 1 == getItemCount() ? footerType : songType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ViewHolder类,用于歌曲列表项。
|
|
|
|
|
*/
|
|
|
|
|
class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
|
|
TextView titleTv;
|
|
|
|
@ -109,13 +147,19 @@ public class AlbumSongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* FooterHolder类,用于底部视图。
|
|
|
|
|
*/
|
|
|
|
|
class FooterHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
FooterHolder(View itemView) {
|
|
|
|
|
super(itemView);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判断点击的是否为上一个点击的项目
|
|
|
|
|
/**
|
|
|
|
|
* 处理点击事件,更新上一次点击的位置。
|
|
|
|
|
* @param position 当前点击的位置
|
|
|
|
|
*/
|
|
|
|
|
private void equalPosition(int position) {
|
|
|
|
|
if (position != mLastPosition) {
|
|
|
|
|
if (mLastPosition != -1) notifyItemChanged(mLastPosition);
|
|
|
|
@ -123,4 +167,4 @@ public class AlbumSongAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|
|
|
|
}
|
|
|
|
|
notifyItemChanged(position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|