如何在回收者视图项目之间进行通信

时间:2018-09-12 09:21:57

标签: android android-recyclerview

我有一个Recycler视图。每个项目视图持有者都有一个媒体播放器实例。因此,基本上,回收者视图会显示该视频项目的列表。 每当用户开始播放一个项目时,我都想停止当前正在播放的其他任何项目。

我想知道如何获取当前正在播放内容的视图的视图持有者实例,以便可以在开始播放新项目之前停止播放它。

6 个答案:

答案 0 :(得分:1)

只是Media Player的一个用户实例,当您开始播放视频时,只需检查媒体播放器是否正在播放,然后停止当前播放的视频并开始播放所需的视频即可。

答案 1 :(得分:1)

按照以下步骤

  1. 在适配器中命名为mCurrentPlayingPos = -1的一个变量。
  2. 在开始播放视频时在点击或事件上应用逻辑。如下所示

    if (mCurrentPlayingPos != -1) {
        // Find view holder using this position. If view holder is not null, then use method to release 
        // player as given mCurrentPlayingPos. 
        // If viewholder is null, that is recycled to no need to do anything. 
    } else {
        // This would be very first case    
        // Set value for mCurrentPlayingPos with positionInAdapter's value 
    }
    // Setup media player and play video below
    

答案 2 :(得分:0)

int sSelectedPos = 0;

// call this on play button click 

     for (ModelMusic data : itemList) {
     if (itemList.indexOf(data) == position) {
      sSelectedPos = position;
     data.setPlaying(true);
     } else {
      data.setPlaying(false);
     }
      notifyDataSetChanged();
     }

//POJO class to manage your play state of song.
    public class ModelMusic {
        private boolean isPlaying = false;

        public boolean isPlaying() {
            return isPlaying;
        }

        public void setPlaying(boolean playing) {
            isPlaying = playing;
        }
    }

答案 3 :(得分:0)

您可以使用两个变量来存储上一次点击和最近点击,

int pre=-1,recent=-1;boolean clicked=false;

在您的 onClick()方法中,使用

clicked=true;
pre=recent;
recent=clickedposition;
if(pre!=-1)
this.notifyItemChanged(pre);
this.notifyItemChanged(recent);

并在您的 onBindViewHolder()中添加此

 if(clicked)
 { 
    if(pre!=-1 && pos==pre)
      stop(pre);//your code to stop the video
    if(pos==recent)
      {
          playvideo(recent);
          clicked=false;
      }
 }

答案 4 :(得分:0)

这是我的想法,请尝试实施; 为每个recyclerView项目创建一个state属性 例如isPlaying,isPaused

public class Video {
   String url;
   boolean isPlaying;

   public void setPlaying(boolean isPlaying){
          this.isPlaying = isPlaying;
   } 

   public boolean isPlaying(){
          return isPlaying;
   }

}

当视频开始播放时,将isPlaying设置为true

public VideoAdapter extends RecyclerView.Adapter<VideoViewHolder>{
     Context ctx;
     List<Video> videos;

     public VideoAdapter(Context ctx, List<Videos> videos){
           this.ctx = ctx;
           this.videos = videos;
     }

      @Override
      public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

          View view = LayoutInflater.from(mContext).inflate(R.layout.video_view, parent, false);
          VideoViewHolder holder = new VideoViewHolder(view);
          return holder;
      }

      @Override
      public void onBindViewHolder(VideoViewHolder holder, int position) {
          holder.bind(videos.get(position));
          holder.setOnClickListener(this);
      }

      @Override
      public void onClick(View v) {
            int adapterPosition = getAdapterPosition();
            Video videos = video.get(adapterPosition)

            if (video.isPlaying()) {
                video.setPlaying(false);
            } else {
                video.setPlaying(true);
            }

            // Notify data set change will reload all view holders and play where playing is true, and cancel where cancel is false
            notifyDataSetChanged();
         }
  }




class VideoViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
VideoView videoView;

VideoViewHolder(View itemView) {
    super(itemView);
    videoView = (VideoView) itemView.findViewById(R.id.videoView);
    itemView.setOnClickListener(this);
}

public void bind(Video video) {
    if (video.isPlaying()) {
        video.play();
    } else {
        videoView.stop();
    }
}

答案 5 :(得分:0)

https://github.com/arthur3486/ARVI 参考这个美丽的项目你会得到你的答案