Search code examples
androidandroid-viewpagerfullscreen

ViewPager to display a full screen gallery with images of different sizes


I try to make a slideshow gallery using the viewPager component. The normal view is OK but I don't know how to display full screen images of different sizes, since it is not possible to set the ViewPager height to WRAP_CONTENT.

I've seen that this is because all the pictures are not loaded when the component is built, and that a solution consist in browsing all the images (looping until getChlidCount()) and to use the height of the larger image, but I can't because the other pictures will be deformed.

Can someone please help me ? I looked everywhere but still no solution. How can it be so complicated to do something that seems so simple ?

I'm not sure to be able to post images, so this is a schematic description of my problem.

http://nsa38.casimages.com/img/2015/07/07/150707040457377832.png

Thanks


Solution

  • I found the solution by myself. It's very simple actually.

    Within the ViewPager adapter, do not directly add the imageView to the viewpager, but instead add your imageView to a linearLayout and then add the linearLayout to your viewPager.

    Which means, something like :

    ImageView iv = new ImageView();
    LinearLayout ll = new LinearLayout();
    
    ll.addView(iv,0);
    viewPager.addView(ll,0);