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.My list has 14 items(images and videos). but linearlayout.addview shows repeated images only. after showing 5 images, it start from 1. 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);
}
}
}
@Override
public int getItemCount() {
return itemList.size();
}
public class SlideViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
private List<Integer> poslist = new ArrayList<>();
private int i = 0;
public SlideViewHolder(@NonNull View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.llout_slidescreenId);
}
}
This is happening because you keep 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. Write this as first line inside onBindViewHolder()
holder.linearLayout.removeAllViews()
NOTE- You should make few modification to your adapter code .
Do not create view at runtime Since there are only 2 views VideoView
and ImageView
. have them added in R.layout.layout_slidescreen
and Change their Visibility accordingly.
Do not use BitmapFactory.decodeFile
directly inside bind
it should be called from a background thread. This will make your UI laggy. Use Some ImageLoader lib Glide
maybe.