【问题标题】:Why viewpager2 display same images repeatedly after 5 images from a list.why?为什么 viewpager2 在列表中的 5 张图像后重复显示相同的图像。为什么?
【发布时间】:2020-10-18 07:28:00
【问题描述】:

我正在使用 viewpager2 创建一个图像和视频滑块。我为此使用了recyclerviewadapter。我的代码如下。文件中的位图正确加载到 imageview。我的列表有 14 个项目(图像和视频)。但 linearlayout.addview 只显示重复的图像。显示 5 张图像后,它从 1 开始。为什么这些图像重复..请帮助..我的 recylerview 适配器类如下..这有什么问题

 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);


        }
    }

【问题讨论】:

  • 谢谢AF先生..我从这里得到了答案...非常感谢..

标签: android android-recyclerview android-viewpager2


【解决方案1】:

发生这种情况是因为您不断在 onBindViewHolder() 中添加视图,并且从未删除已删除的前一个视图。这背后的原因是RecyclerView.Adapter 重复使用同一个视图持有者。将此写为onBindViewHolder()中的第一行

holder.linearLayout.removeAllViews()

注意-您应该对适配器代码进行少量修改。

  1. 不要在运行时创建视图因为只有 2 个视图 VideoViewImageView 。将它们添加到 R.layout.layout_slidescreen 并相应地更改它们的可见性。

  2. 不要在bind 内直接使用BitmapFactory.decodeFile,它应该从后台线程调用。这将使您的 UI 变得迟钝。可能使用一些 ImageLoader lib Glide

【讨论】:

  • 我该如何表达我的感激之情...ADM先生...您太棒了...非常感谢...爱...
  • 先生,我听从了您的建议。我将视图添加到布局中...现在正在加载图像和视频..但是在滑动过程中,只出现了几个 mp4 视频文件..但是从近视图或直接选择中选择时,视频可以正常工作..什么会错的..请帮忙..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 2012-12-17
  • 2020-11-22
相关资源
最近更新 更多