maurycyw / StaggeredGridView

A modified version of Android's experimental StaggeredGridView. Includes own OnItemClickListener and OnItemLongClickListener, selector, and fixed position restore.
https://github.com/maurycyw/StaggeredGridViewDemo
1.64k stars 664 forks source link

SGV.onMeasure() does not recalculate mColumnTops/Bottoms/Lefts when SGV dimension changes #89

Open vicianm opened 10 years ago

vicianm commented 10 years ago

How to reproduce the problem:

Place SGV e.g. inside LinearLayout together with button B.

When button B is clicked change visibility of B to GONE. This way the SGV dimension is increased but SGV children are not layed out properly.

Quick (dirty) fix: recalculate mColumnTops/Bottoms/Lefts each time SGV.onMeasure() is called:

@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (mColumnCount <= 0) {
        boolean isLandscape = getMeasuredWidth() > getMeasuredHeight();
        mColumnCount = isLandscape ? mColumnCountLandscape : mColumnCountPortrait;
    }

    // our column width is the width of the listview
    // minus it's padding
    // minus the total items margin
    // divided by the number of columns
    mColumnWidth = calculateColumnWidth(getMeasuredWidth());

    //if (mColumnTops == null || mColumnTops.length != mColumnCount) {
        mColumnTops = new int[mColumnCount];
        initColumnTops();
    //}
    //if (mColumnBottoms == null || mColumnBottoms.length != mColumnCount) {
        mColumnBottoms = new int[mColumnCount];
        initColumnBottoms();
    //}
    //if (mColumnLefts == null || mColumnLefts.length != mColumnCount) {
        mColumnLefts = new int[mColumnCount];
        initColumnLefts();
    //}
}
hugogz commented 10 years ago

Please could you explain with a bit more detail this fix? I'm really interested in it but i have no idea where are some functions like initColumnTops() or calculateColumnWidth in the original SGV. Could you post the whole code please??

JGeraldoLima commented 9 years ago

just comment these lines didn't work for me :/ The problem occurs only if pull down the list when it is already on the last child. So all the itens disapears or gets messy