简体   繁体   English

当我向下和向上滚动 RecyclerView 时,Viewpager 图像消失

[英]Viewpager images are disappear when i scroll down and up the RecyclerView

I want to use ViewPager inside of RecyclerView(for multiple images like instagram).我想在 RecyclerView 中使用 ViewPager(对于像 instagram 这样的多个图像)。 But when I scroll down and up the RecyclerView, the images are disappear.但是当我向下和向上滚动 RecyclerView 时,图像消失了。 I think my problem is create a new Adapter inside of onBindViewHolder method.我认为我的问题是在 onBindViewHolder 方法中创建一个新的适配器。 How can I create Adapter for once in ViewPagerAdapter?如何在 ViewPagerAdapter 中创建一次适配器? Is there a solution for this?有解决方案吗?

public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {

    switch (holder.getItemViewType()){


        case 1:
        {
            final ViewHolderMultiple viewHolderMultiple=(ViewHolderMultiple)holder;

            viewHolderMultiple.user_name.setText(list.get(position).getUsername());


            Call<List<DetailPojo>> call = restApiClass.getImages(list.get(position).getId());
            call.enqueue(new Callback<List<DetailPojo>>() {
                @Override
                public void onResponse(Call<List<DetailPojo>> call, Response<List<DetailPojo>> response) {

                    if (response.isSuccessful()) {

                        if (response.body() != null) {

                            imagesList = response.body();
                            postSliderAdapter = new PostSliderAdapter(imagesList, context); //I think my problem is here
                            viewHolderMultiple.postsCircleIndicator.setViewPager(viewHolderMultiple.postsViewPager);

                            Log.i("imageList",imagesList.size()+"");

                        }

                    }

                }
                @Override
                public void onFailure(Call<List<LessonPostImagesDetailPojo>> call, Throwable t) {

                    Toast.makeText(context, "Connection Error", Toast.LENGTH_SHORT).show();
                }
            });

        }
        break;
        default: break;
    }

}

I changed the ViewPager height to hardcode(400dp) from wrap_content and it works.我将 ViewPager 高度从 wrap_content 更改为硬编码(400dp),它可以工作。 I was trying to change ViewPager height dynamically according to images size, so my ViewPager size was wrap_content.我试图根据图像大小动态更改 ViewPager 高度,所以我的 ViewPager 大小是 wrap_content。 So problem was wrap_content.所以问题是wrap_content。 But I didn't understand why.但我不明白为什么。 It's too strange.太奇怪了。

Hi @Mehmet Pls use this method.. rv.getRecycledViewPool().setMaxRecycledViews(0, 0);嗨@Mehmet 请使用这个方法.. rv.getRecycledViewPool().setMaxRecycledViews(0, 0); it may be helpful.. see this link for method description..它可能会有所帮助..有关方法说明,请参阅链接..

1st of all dont call api in onbindviewholder create a separate function of Api calling in activity and in bindviewholder call that function like this: ((MainActivity)context).funGetData(); 1st of all dont call api in onbindviewholder create a separate function of Api calling in activity and in bindviewholder call that function like this: ((MainActivity)context).funGetData(); before calling api check if your arraylist is not empty then call api else show data from your previously loaded arraylist.在调用 api 之前检查您的 arraylist 是否不为空,然后调用 api 否则显示来自您之前加载的 ZEB3A234246F50C925 的数据make getdata public in your main activity and set adapter to viewpager in onsuccess method as you have done.在您的主要活动中公开 getdata 并在 onsuccess 方法中将适配器设置为 viewpager,就像您所做的那样。 replace switch with if condition in onbindview holder and dont forget to write its else part.用 onbindview 持有者中的 if 条件替换 switch 并且不要忘记写它的 else 部分。 After making these changes let me know.进行这些更改后,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Glide和RecyclerView在上下滚动时图像消失,图像之间的空间增加 - Images disappear when scrolling up and down and spaces increase between images using Glide and RecyclerView 当我在Recyclerview回收中向下滚动时不能正常工作 - when i scroll up an down in Recyclerview recycling not working properly Recyclerview图像向上/向下滚动时放错位置并消失 - Recyclerview Images Misplacing and disappear while scrolling up/down 向上或向下滚动时,recyclerView 中的 Viewpager 消失 - Viewpager inside recyclerView dissapears when scrolling up or down Recyclerview不显示数据,直到我在带有两个片段的ViewPager中向上滚动 - Recyclerview not showing data untill i scroll up in a viewpager with two fragments 当我们上下滚动 recyclerView 时隐藏和显示视图组 - Hide and show viewgroup when we scroll up and down the recyclerView 当我将水平Recyclerview滚动到垂直Recyclerview时,Nestedscrollview会跳起来 - Nestedscrollview jump up when I scroll horizontal recyclerview into vertical recyclerview 当我向下或向上滚动时Listview崩溃 - Listview crashes when I scroll down or up 向下滚动后,RecyclerView中的图像数量错误 - Wrong number of images in RecyclerView after scroll down 嵌套的 RecyclerView 滚动无法向下滚动 ViewPager2 的 BottomSheetBehavior - BottomSheetBehavior with ViewPager2 can't be scrolled down by nested RecyclerView scroll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM