This is my idea, try to implement it;
Create a state attribute for each recyclerView item
example isPlaying, isPaused
public class Video {
String url;
boolean isPlaying;
public void setPlaying(boolean isPlaying){
this.isPlaying = isPlaying;
}
public boolean isPlaying(){
return isPlaying;
}
}
When the video starts to play set isPlaying to 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();
}
}