Image & Video slider using Viewpager2 , some videos appeared missing in sliding

943 Views Asked by At

I am working in a image and video slider. I can load my images and videos in a viewpager 2. but now, images are working correct, but during sliding video will only be appeared sometimes..otherwise next image takes its place..what is wrong ? Please..i am newbie here..

 public void onBindViewHolder(@NonNull final SlideViewHolder holder, int position) {
        //holder.linearLayout.removeAllViews();

        Log.d("CHKck", position + " " + getItemCount());
        StatusModel statusModel = itemList.get(position);
        File file = statusModel.getFile();
        Log.d("CHKfileadapt", String.valueOf(file) + " " + position);
        if (file.exists()) {
            if (statusModel.isVideo) {
                holder.slideVideo.setVisibility(View.VISIBLE);
                holder.slideVideo.setVideoPath(file.getAbsolutePath());
                MediaController mediaController = new MediaController(context);
                mediaController.setAnchorView(holder.slideVideo);
                holder.slideVideo.setMediaController(mediaController);
                holder.slideVideo.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.setLooping(true);
                        holder.slideVideo.start();
                    }
                });
            } else {
                holder.slideImage.setVisibility(View.VISIBLE);
                Glide.with(context).load(file).into(holder.slideImage);
            }
        }
    }
1

There are 1 best solutions below

0
On

This is happening because i kept adding view inside onBindViewHolder() and never removed the removed the previous one. The reason behind this is RecyclerView.Adapter reuses the same view holder repeatedly.It was a previous advice I got from Mr.ADM here..So I initialized all views inside onbindingviewholder..and thus the problem resolved..thank you