我们正在为一个项目制作一个Android的MusicPlayer。我们有3个标签 - 歌曲(显示手机上的所有歌曲),正在播放(应显示当前排队的歌曲)和播放列表。
排队的歌曲不会出现在NowPlayingFragment中。 我为我目前排队的歌曲实施了RecyclerView。歌曲通过SongsRecyclerAdapter(处理"歌曲"标签中的列表)排队。我在MusicProvider类中使用静态addToQueue()方法,它将歌曲添加到List currentQueue(在同一个类中) 并且恰好在来自适配器的NotifyDataSetChanged()之后用于入队列表。 适配器本身对其项目使用相同的List currentQueue。 这是我的enqueueAdapter
public class NowPlayingRecyclerAdapter extends RecyclerView.Adapter<NowPlayingRecyclerAdapter.ViewHolder>{
private Context context;
public List<MediaMetadataCompat> mSongs;
private LayoutInflater inflater;
public NowPlayingRecyclerAdapter(Context context,List<MediaMetadataCompat> songs){
this.context = context;
this.mSongs = songs;
this.inflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.song_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
MediaMetadataCompat currentSong = mSongs.get(position);
holder.setData(currentSong,position);
holder.setListeners();
}
private void removeSongFromQueue(int position) {
mSongs.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,getItemCount());
}
@Override
public int getItemCount() {
return mSongs.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
LinearLayout item;
TextView title;
TextView artist;
ImageView deleteIcon;
int position;
MediaMetadataCompat current;
ImageView dotsIcon;
private ViewHolder(View itemView) {
super(itemView);
this.item = (LinearLayout) itemView.findViewById(R.id.song_item);
this.artist = (TextView) itemView.findViewById(R.id.artist);
this.title = (TextView) itemView.findViewById(R.id.title);
this.deleteIcon = (ImageView) itemView.findViewById(R.id.img_delete);
this.dotsIcon = (ImageView) itemView.findViewById(R.id.img_dots);
dotsIcon.setVisibility(View.GONE);
}
private void setData(MediaMetadataCompat currentSong, int position) {
this.current = currentSong;
this.title.setText(currentSong.getDescription().getTitle());
this.artist.setText(currentSong.getDescription().getSubtitle());
this.position = position;
}
private void setListeners() {
deleteIcon.setOnClickListener(ViewHolder.this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.img_delete) {
removeSongFromQueue(position);
//removeSongFromPlaylistFile();
}
}
}
这是我的NowPlaingFragment(用于入队)
public class NowPlayingFragment extends Fragment {
public static NowPlayingRecyclerAdapter adapter;
private Context context;
public static List<MediaMetadataCompat> songs;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.now_playing_fragment, container, false);
context = getActivity();
songs = MusicProvider.getCurrentQueue();
context = getActivity();
setUpRecyclerView(view);
return view;
}
private void setUpRecyclerView(View view) {
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_queue);
recyclerView.setHasFixedSize(true);
adapter = new NowPlayingRecyclerAdapter(context,songs);
//adapter = new SongsRecyclerAdapter(view.getContext(),songs,true);
recyclerView.setAdapter(adapter);
LinearLayoutManager myLinearLayoutManager = new LinearLayoutManager(view.getContext());
myLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(myLinearLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(context, myLinearLayoutManager.getOrientation()));
}
这就是我在一个点击收听器中排队的歌曲
MusicProvider.addToQueue(songs.get(getAdapterPosition())
.getMediaId());
NowPlayingRecyclerAdapter myAdapter = NowPlayingFragment.adapter;
myAdapter.notifyDataSetChanged();
MusicProvider类中的重要内容:
public static void addToQueue(String mediaId) {
if (currentQueue == null)
currentQueue = new ArrayList<>();
currentQueue.add(getSong(mediaId));
}
public static List<MediaMetadataCompat> getCurrentQueue() {
return currentQueue != null ? currentQueue : Collections.<MediaMetadataCompat>emptyList();
}
答案 0 :(得分:0)
从适配器和片段内的歌曲中删除静态。 MediaProvider也充满了静态。 您应该通过实例名称引用或创建单例并通过getInstance引用。
答案 1 :(得分:0)
只需致电
notifyDataSetChanged();
or
notifyItemChanged(position);