[英]RecyclerView doesnt visualise/update
我們正在將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();
}
從適配器和片段中的歌曲中刪除靜電。 MediaProvider也充滿了靜電。 您應該按實例名稱引用或創建單例並按getInstance引用。
只是打電話
notifyDataSetChanged();
or
notifyItemChanged(position);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.