siralam / LoopingViewPager

A ViewPager and PagerAdapter combination that support auto scroll, infinite loop and page indicators.
532 stars 64 forks source link

When loading images from Url Second image does not load #1

Closed stshelton closed 6 years ago

stshelton commented 6 years ago

Hello, I am trying to use your library to display images through glide. The first image loads fine but once I scroll to the next page the second image and third image does not load. Once view pager returns to first index (infinite loop) the first image is displayed again. I used your tutorial to build the adapter, here is the code.

` public class LoopingViewPageAdapter extends LoopingPagerAdapter{

private static final String TAG = "LoopingViewPageAdapter";

public LoopingViewPageAdapter(Context context, ArrayList<String> itemList, boolean isInfinite) 
{
    super(context, itemList, isInfinite);
}

@Override
protected View getItemView(View convertView, int listPosition, ViewPager container) {

    if(convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.item_viewpager_image,null);
        container.addView(convertView);
    }
    Log.i(TAG, "getItemView: " + itemList.get(listPosition));
    ImageView image = convertView.findViewById(R.id.imageOfItem);
    UIHelper.setImageWithURL(context, itemList.get(listPosition), image );
    return convertView;
}

} `

//setImage with URL public static void setImageWithURL(Context context, String url, ImageView imageView) { RequestOptions options = new RequestOptions().centerCrop(); Glide.with(context).load(url).apply(options).into(imageView); }

One more thing, is there an on touch listener for the view pager?

siralam commented 6 years ago

Although not using Glide, I tested the library with Fresco before and works well.
Just to make sure, you have internet permission declared, right? (Cause I forgot this at the beginning as well)

Anyway, if I have time later, I will try to test using Glide again.

One more thing, is there an on touch listener for the view pager?

I didn't set anyone for LoopingViewPager. But since LoopingViewPager itself is a View, just supply one by yourself.

But I guess, you are actually asking if there is an onItemClickListener for the ViewPager. If that is the case, I expect user to create their own in the Adapter.
In short, I expect all item related things are inside your own Adapter.

e.g.

private View.OnClickListener clickListener = new View.OnClickListener {...}

@Override
protected View getItemView(View convertView, int listPosition, ViewPager container) {

    ...
    convertView.setOnClickListener(clickListener);
}
stshelton commented 6 years ago

Thanks for the reply, To be honest, I woke up today cleaned the project and the second and third images loaded. Didn't touch the code, don't know why it wasn't working the other night. If anything comes up again ill let you know. Great library by the way, and yea that makes sense for the onItemClickListener. Thanks Again.

siralam commented 6 years ago

Thank you :) You are welcomed!

siralam commented 6 years ago

@stshelton Hey, I can reproduce the problem today. It does exists.

The reason lies in the way of implementing wrap_content of ViewPager. My implementation is using this post, where the viewpager resizes itself according to the height of the first child.

And because loading network images into a wrap_content ImageView, its height will be 0 at the beginning until it is loaded. But onMeasure() won't be triggered after the image has been loaded into the view.

The way I approach this problem is to add an aspect ratio attribute to the ViewPager, since it makes good sense that a ViewPager should load images that are of the same aspect ratio.

This feature will be added very soon in v1.0.5.

Thanks!