500px / greedo-layout-for-android

Full aspect ratio grid LayoutManager for Android's RecyclerView
MIT License
1.64k stars 156 forks source link

Add on-demand ratio calculation #6

Closed ajalt closed 8 years ago

ajalt commented 8 years ago

This is a cool library. Is it possible to use without loading images before they're displayed? In the sample app you use Picasso to load images, but you have to load all the images manually anyway to calculate their ratios, which seems to defeat the point of using an image loader.

If you wanted to use Picasso to load images from the network, for example, would that be impossible with this library?

AngleV commented 8 years ago

@ajalt I suppose it would work perfect if you pre know the ratios of each image.As you may notice 500px app loads a gray placeholder with the correct size and then waits for the image to come

JVillella commented 8 years ago

The point of this LayoutManager is to lay images out according to their aspect ratios. The layout manager is not concerned where or how you get the aspect ratio of the image, all it does is query it following the interface below:

public interface SizeCalculatorDelegate {
    double aspectRatioForIndex(int index);
}

This design is how we make what you are asking possible. The images are not needed before we lay them out. Only their aspect ratios are. @AngleV on the 500px app the placeholders are the same size as the corresponding image, but we are waiting for the image to download before we can show it. This is no different than your typical setup.

@ajalt to answer your question about using this library with images from the network - no it's not impossible, it's what we do of course :smile:. Whatever API you are using to fetch the images needs to return its width and height ahead of time that way the layout can size the items, add placeholders, then show the image when it has arrived. If your API doesn't do this, then yes, you will need to wait for the set of images to comeback so we can grab their sizes and begin the layout process. Hope this helps!

ajalt commented 8 years ago

Alright, that's what I suspected. Thanks for the reply.