0

I have a recycler view being populated by data from a server...

    //Instantiate the list
    final RecyclerView feed_list = (RecyclerView)view.findViewById(R.id.feed_list);
    //Create instance of Linear Layout Manager
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    //Apply Layout Manager to RecyclerView
    feed_list.setLayoutManager(llm);
    //Set fixed size
    feed_list.setHasFixedSize(true);

    ContentFeedAdapter adapter = new ContentFeedAdapter(getActivity(),iContentPage.getContent());
                feed_list.setAdapter(adapter);

There are 10 different layout variations for the recycler view, which I managed to sort out that's to this answer.

The problem is as part of some of these items I have video and audio, media items.

So in my onBindViewHolder method if it's a media item I create an instance of my custom media player

    @Override
    public void onBindViewHolder(final AbstractHolder holder, int position) {
        final IFeedContent content = mFeedContentList.get(position);
        holder.bindData(content);


        if(content.getMedia()!=null){
            String mimeType = content.getMedia().getMimeType();
            if(mimeType.contains("video") || mimeType.contains("audio")){
                final ProgressBar progressBar = holder.getProgress();
                final ImageView playButton = holder.getPlayImage();
                mMediaPlayer = holder.getMediaPlayer();

                playButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMediaPlayer.startVideo(content.getMedia().getMediaUrl());
                        holder.getPlayImage().setVisibility(View.GONE);
                        progressBar.setVisibility(View.VISIBLE);
                    }
                });
            }
        }
}

The problem is, when I click the play button on the first item, the audio will play in the second with the media controller etc. I think the issue that as there will be two viewholders bound at the same time, when I click the play button, it's the second view holder that it works with.

I'm not sure if this is the issue but if anyone could help me I'd appreciate it or needs to see more code, let me know

Community
  • 1
  • 1
DJ-DOO
  • 4,545
  • 15
  • 58
  • 98
  • what if you directly accees playimage from holder object instead of getPlayImage().. Does it matter in any way ? – Ramesh Aug 26 '15 at 10:45
  • @Ramesh It doesn't make a difference I'm afraid – DJ-DOO Aug 26 '15 at 11:01
  • is mMediaPlayer a class member ? mMediaPlayer is being updated whenever bindViewHolder is getting updated. so it will be having reference to last bindViewHolder that is called. I am suspecting this is the cause !! – Ramesh Aug 26 '15 at 11:16

1 Answers1

0

mMediaPlayer which is a class member is being update on each bind view holder call. So whenever mMediaPlayer will be holding reference to mediaPlayer from last bindViewHolder call. Make it local to bind view holder and try to see if your bug is fixed. Please check if it gets fixed by this.

Ramesh
  • 1,287
  • 1
  • 9
  • 14