简体   繁体   English

RecyclerView不可视化/更新

[英]RecyclerView doesnt visualise/update

We are making a MusicPlayer for android as a project . 我们正在将Android的MusicPlayer作为一个项目。 We have 3 tabs - Songs(displaying all songs on phone),Now playing(should display currently enqueued songs) and Playlists. 我们有3个标签-歌曲(在电话上显示所有歌曲),正在播放(应显示当前排队的歌曲)和播放列表。

The enqueued songs dont appear in the NowPlayingFragment. 入队的歌曲不会出现在NowPlayingFragment中。 I implemented a RecyclerView for my currently enqueued songs . 我为当前排队的歌曲实现了RecyclerView。 The songs are enqueued through the SongsRecyclerAdapter(which handles the list in the "Songs"tab) . 通过SongsRecyclerAdapter(处理“歌曲”标签中的列表)将歌曲排入队列。 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. 我在MusicProvider类中使用静态addToQueue()方法,该方法将歌曲添加到List currentQueue(在同一类中)中,并且紧接在已排队列表的适配器的NotifyDataSetChanged()之后。 The adapter itself uses the same List currentQueue for its items. 适配器本身对其项目使用相同的List currentQueue。 Here is my enqueueAdapter 这是我的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) 这是我的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()));
}

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

Remove the static from adapter and from songs inside the fragment. 从适配器和片段中的歌曲中删除静电。 MediaProvider is full of statics too. MediaProvider也充满了静电。 You should refer by instance name or create singleton and refer by getInstance. 您应该按实例名称引用或创建单例并按getInstance引用。

just call 只是打电话

 notifyDataSetChanged();
 or 
 notifyItemChanged(position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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