|
|
|
|
@ -19,35 +19,49 @@ import com.example.musicplayer.util.FileUtil;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* HistoryAdapter 是一个 RecyclerView.Adapter 的扩展,用于展示用户播放历史的歌曲列表。
|
|
|
|
|
* Created by 残渊 on 2018/12/2.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
|
|
|
|
private static final String TAG = "HistoryAdapter";
|
|
|
|
|
private int footerViewType = 1;
|
|
|
|
|
private int itemViewType = 0;
|
|
|
|
|
private List<HistorySong> mHistoryList;
|
|
|
|
|
private Context mContext;
|
|
|
|
|
private int mLastPosition = -1;
|
|
|
|
|
private OnItemClickListener onItemClickListener;
|
|
|
|
|
private int footerViewType = 1; // 底部视图类型标识
|
|
|
|
|
private int itemViewType = 0; // 列表项视图类型标识
|
|
|
|
|
private List<HistorySong> mHistoryList; // 播放历史歌曲列表
|
|
|
|
|
private Context mContext; // 上下文环境
|
|
|
|
|
private int mLastPosition = -1; // 上一个被点击的位置
|
|
|
|
|
private OnItemClickListener onItemClickListener; // 点击事件监听器
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置点击事件的监听器。
|
|
|
|
|
* @param onItemClickListener 点击事件监听器
|
|
|
|
|
*/
|
|
|
|
|
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
|
|
|
|
|
this.onItemClickListener = onItemClickListener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,初始化上下文环境和播放历史歌曲列表。
|
|
|
|
|
* @param context 上下文环境
|
|
|
|
|
* @param historyList 播放历史歌曲列表
|
|
|
|
|
*/
|
|
|
|
|
public HistoryAdapter(Context context, List<HistorySong> historyList) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mHistoryList =historyList;
|
|
|
|
|
mHistoryList = historyList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ViewHolder类,用于歌曲列表项。
|
|
|
|
|
*/
|
|
|
|
|
class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
TextView songNameTv;
|
|
|
|
|
TextView singerTv;
|
|
|
|
|
RippleView mItemView;
|
|
|
|
|
View playLine;
|
|
|
|
|
|
|
|
|
|
TextView songNameTv; // 歌曲名称
|
|
|
|
|
TextView singerTv; // 歌手名称
|
|
|
|
|
RippleView mItemView; // 带有水波纹效果的视图
|
|
|
|
|
View playLine; // 播放线条,表示当前播放的歌曲
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,初始化歌曲列表项的视图。
|
|
|
|
|
* @param itemView 歌曲列表项的视图
|
|
|
|
|
*/
|
|
|
|
|
public ViewHolder(View itemView) {
|
|
|
|
|
super(itemView);
|
|
|
|
|
songNameTv = itemView.findViewById(R.id.tv_title);
|
|
|
|
|
@ -58,18 +72,27 @@ public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 底部holder
|
|
|
|
|
* FooterHolder类,用于底部视图。
|
|
|
|
|
*/
|
|
|
|
|
static class FooterHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
TextView numTv; // 底部提示文本
|
|
|
|
|
|
|
|
|
|
TextView numTv;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,初始化底部视图的视图。
|
|
|
|
|
* @param itemView 底部视图的视图
|
|
|
|
|
*/
|
|
|
|
|
public FooterHolder(View itemView) {
|
|
|
|
|
super(itemView);
|
|
|
|
|
numTv = itemView.findViewById(R.id.tv_song_num);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建新的ViewHolder实例。
|
|
|
|
|
* @param parent 父布局
|
|
|
|
|
* @param viewType 视图类型
|
|
|
|
|
* @return 返回对应的ViewHolder
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
|
if (viewType == itemViewType) {
|
|
|
|
|
@ -85,6 +108,11 @@ public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将数据绑定到ViewHolder上。
|
|
|
|
|
* @param viewHolder 当前ViewHolder
|
|
|
|
|
* @param position 当前位置
|
|
|
|
|
*/
|
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
|
|
@Override
|
|
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
|
|
|
|
|
@ -94,10 +122,10 @@ public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
|
|
|
|
|
|
|
|
|
holder.songNameTv.setText(history.getName());
|
|
|
|
|
holder.singerTv.setText(history.getSinger());
|
|
|
|
|
//根据点击显示
|
|
|
|
|
// 根据当前播放的歌曲设置不同的显示效果
|
|
|
|
|
if(history.getSongId().equals(FileUtil.getSong().getSongId())){
|
|
|
|
|
holder.playLine.setVisibility(View.VISIBLE);
|
|
|
|
|
mLastPosition =position;
|
|
|
|
|
mLastPosition = position;
|
|
|
|
|
holder.songNameTv.setTextColor(mContext.getResources()
|
|
|
|
|
.getColor(R.color.yellow));
|
|
|
|
|
holder.singerTv.setTextColor(mContext.getResources()
|
|
|
|
|
@ -119,7 +147,10 @@ public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判断点击的是否为上一个点击的项目
|
|
|
|
|
/**
|
|
|
|
|
* 处理点击事件,更新上一次点击的位置。
|
|
|
|
|
* @param position 当前点击的位置
|
|
|
|
|
*/
|
|
|
|
|
public void equalPosition(int position) {
|
|
|
|
|
if (position != mLastPosition) {
|
|
|
|
|
notifyItemChanged(mLastPosition);
|
|
|
|
|
@ -128,13 +159,22 @@ public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
|
|
|
|
notifyItemChanged(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回总的条目数,包括底部视图。
|
|
|
|
|
* @return 总条目数
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public int getItemCount() {
|
|
|
|
|
return mHistoryList.size() + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据位置返回不同的视图类型。
|
|
|
|
|
* @param position 位置
|
|
|
|
|
* @return 视图类型
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
|
return position + 1 == getItemCount() ? footerViewType : itemViewType;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|