简体   繁体   中英

RecyclerView doesnt visualise/update

We are making a MusicPlayer for android as a project . We have 3 tabs - Songs(displaying all songs on phone),Now playing(should display currently enqueued songs) and Playlists.

The enqueued songs dont appear in the NowPlayingFragment. I implemented a RecyclerView for my currently enqueued songs . The songs are enqueued through the SongsRecyclerAdapter(which handles the list in the "Songs"tab) . I use the static addToQueue() method in the MusicProvider class which adds songs to a List currentQueue(in the same class) and right after NotifyDataSetChanged() from the adapter for the enqueued list. The adapter itself uses the same List currentQueue for its items. Here is my 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();
        }
    }

}

And Here is my NowPlaingFragment(for the enqueue)

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()));
}

This is what I do in a click listener to enqueue songs

    MusicProvider.addToQueue(songs.get(getAdapterPosition())
                                        .getMediaId());
    NowPlayingRecyclerAdapter myAdapter = NowPlayingFragment.adapter;

                                myAdapter.notifyDataSetChanged();

The important stuff in MusicProvider class :

    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();
}

Remove the static from adapter and from songs inside the fragment. MediaProvider is full of statics too. You should refer by instance name or create singleton and refer by getInstance.

just call

 notifyDataSetChanged();
 or 
 notifyItemChanged(position);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM