View Pager takes time to post images with Picasso

1

In my mainActivity I have this

viewPager = (ViewPager) findViewById(R.id.viewPager);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
viewPager.setAdapter(viewPagerAdapter);

This is the declaration of the xml where the viewPager is located

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_marginTop="55dp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

This is my adapter:

public class ViewPagerAdapter extends PagerAdapter {

private Context context;
private LayoutInflater layoutInflater;
private Integer [] images = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.g, R.drawable.i, R.drawable.j};

public ViewPagerAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return images.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public Object instantiateItem(ViewGroup container, final int position) {

    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.slider_show, null);
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
    Picasso.with(context).load(images[position]).fit().into(imageView);

    ViewPager vp = (ViewPager) container;
    vp.addView(view, 0);
    return view;

}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

    ViewPager vp = (ViewPager) container;
    View view = (View) object;
    vp.removeView(view);

}
}

I want to know why, when I pass the photos, he shows me a blank image and then he loads the photo? Is not there some way that the viewPager loads the images similar to a photo application? I need help please I could not figure out what it is and I'm going crazy. Thanks in advance.

    
asked by Alex Rivas 10.07.2018 в 17:45
source

1 answer

1

If you are using ViewPager you should take into account that not all the views defined in the pager are loaded, if this will be done and even more loading images on each page could cause memory problems.

By default it only loads a view, if you want to increase the pages that are preloaded you can use the setOffscreenPageLimit ()

As an example you can preload 5 pages, the current two on the left and two on the right, would be done in this way:

mViewPager.setOffscreenPageLimit(5);

Obviously the amount must be equal to or less than the number of pages in the pager. With this you would avoid a little the fact of seeing pages without the image when paging.

It is also advisable to implement saving of the images to disk, with this you would avoid to see the loading of the images from the internet each time a page is instantiated since the disk access is faster.

I add an example of what I mention using the latest version of Picasso that now does not require context. First define .networkPolicy(NetworkPolicy.OFFLINE) to determine if the image exists on the disk, if it does not exist, download the internet:

Picasso.get().load(urlImage).networkPolicy(NetworkPolicy.OFFLINE).into(imageView, new Callback() {
            @Override
            public void onSuccess() {
                Log.i(TAG, "Load image from caché! " + urlImage);
                textView.setText("From Caché:\n " + Uri.parse(urlImage).getLastPathSegment());
            }

            @Override
            public void onError(Exception e) {
                Log.e(TAG, "onError() " + e.getMessage());
                Log.i(TAG, "Try to load image from internet! " + urlImage);               
                //Can´t find image in cache, load from internet.
                Picasso.get().load(urlImage).into(imageView);

            }
        });
    
answered by 11.07.2018 / 23:46
source