确保已经获取了读取外部存储器权限。
创建一个类来包含音频文件的相关信息。
public class AudioModel {
private String title;
private String artist;
private String duration;
private String path;
public AudioModel(String title, String artist, String duration, String path) {
this.title = title;
this.artist = artist;
this.duration = duration;
this.path = path;
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public String getDuration() {
return duration;
}
public String getPath() {
return path;
}
}
public class AudioListAdapter extends RecyclerView.Adapter {
private Context context;
private List audioList;
private OnItemClickListener onItemClickListener;
public AudioListAdapter(Context context, List audioList) {
this.context = context;
this.audioList = audioList;
}
public interface OnItemClickListener {
void onItemClick(AudioModel audioModel);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.onItemClickListener = listener;
}
@NonNull
@Override
public AudioItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_audio, parent, false);
return new AudioItemViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AudioItemViewHolder holder, int position) {
final AudioModel audio = audioList.get(position);
holder.title.setText(audio.getTitle());
holder.artist.setText(audio.getArtist());
holder.duration.setText(audio.getDuration());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(audio);
}
}
});
}
@Override
public int getItemCount() {
return audioList.size();
}
static class AudioItemViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView artist;
TextView