etsy / AndroidStaggeredGrid

An Android staggered grid view which supports multiple columns with rows of varying sizes.
https://github.com/etsy/AndroidStaggeredGrid
4.76k stars 1.13k forks source link

Size grid #160

Open m4rtinsp opened 9 years ago

m4rtinsp commented 9 years ago

Hello everyone! Is there any way to know the height of the grid after you set the adapter?

Thanks!

tibbi commented 9 years ago

you can get it after the view has been drawn, my way is quite ugly, but I do it like

StaggeredGridView meetupGridView = ...
final ViewTreeObserver vto = meetupGridView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT < 16) {
            meetupGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            meetupGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        int height = meetupGridView.getHeight();
    }
});