Your problem comes from the viewer himself. Viewers have a link to the views, while the adapter does not. The adapter saves only data collection. Thus, add a field to the observer to save a link to the data item that you used to populate the view in the view node. In other words:
public class SomeViewHolder extends RecyclerView.ViewHolder{ private View view; private Data data; public SomeViewHolder(View itemView) { super(itemView); view = itemView; } public void bindData(Data data){ view.setData(data); this.data = data; } public void setData(Data data){ this.data = data; } public Data getData(){ return data; } public View getView(){ return view; } }
The viewer now knows which adapter element is using. Therefore, by overriding the binding method in the adapter, you can check whether the holder is already connected to some data, and if the data contains video, you can avoid the binding and force the already loaded view to be installed.
@Override public void onBindViewHolder(SomeViewHolder holder, int position) {
Finally, you will have to override the onViewRecycled method in the adapter, so when the view containing the video is processed, you can get the view and put it in another place.
public void onViewRecycled(SomeViewHolder holder){ if(holder.getData().isVideo()){ videoViewData = holder.getData(). videoView = holder.getView(); videoView.pauseVideo(); } }
remember that this can lead to serious leaks if you do not manage the saved view. In addition, you must define methods for reporting when your data is video, and a properly set equals method.
source share