[英]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.
太奇怪了。
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.