Why viewpager2 display same images repeatedly after 5 images from a list.why?

I am creating a image and video slider using viewpager2. I used recyclerviewadapter for this. My code is below. The bitmaps from file loaded to imageview correctly. but linearlayout.addview shows repeated images only. Why those images are repeating..please help..My recylerview adapter class is given below..Whats wrong with this

 public SlideViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_slidescreen, parent, false);
        return new SlideViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SlideViewHolder holder, int position) {
        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) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                linearLayout.setGravity(Gravity.FILL);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                VideoView videoView = new VideoView(context);
                videoView.setVideoPath(file.getAbsolutePath());
                videoView.setMediaController(new MediaController(context));
                holder.linearLayout.addView(videoView);


            } else {
                Log.d("CHK", "image");
                holder.linearLayout.setOrientation(LinearLayout.VERTICAL);
                holder.linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                ImageView imageView = new ImageView(context);
                Bitmap bitmap = BitmapFactory.decodeFile(statusModel.getPath());
                imageView.setImageBitmap(bitmap);
                imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                holder.linearLayout.addView(imageView);
            }
        }
    }
~~~

Source: Android Questions

LEAVE A COMMENT