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

setColumnCount() problem #76

Open sin3hz opened 10 years ago

sin3hz commented 10 years ago

Hi, when I add setColumnCount(3) follow

    mGridView.addHeaderView(header);
    mGridView.addFooterView(footer);
    mAdapter = new SampleAdapter(this, R.id.txt_line1);

It just show one column, and the item overlay each other.see the screenshot. screenshot_2014-03-18-10-10-15

ptrent commented 10 years ago

Hi, I've been having the same problem. The issue arises when you call setColumnCount, before the view is measured. The width inside the view is 0 and columns widths and positions are calculated wrongly. I've created a hack that resolves that issue, so you can use it untill it is solved.

You have to extend StaggeredGridView and override the onSizeChanged method. This way when the width is larger than 0, you can safely set your column count. Hope it helps.

public class StaggeredGridViewHacked extends StaggeredGridView {
    public StaggeredGridViewHacked(Context context) {
        super(context);
    }
    public StaggeredGridViewHacked(Context context, AttributeSet attributes) {
        super(context, attributes);
    }
    boolean columnCountChanged = false;
    @Override
    protected void onSizeChanged(int w, int h) {
        super.onSizeChanged(w, h);
        if(!columnCountChanged && getWidth() > 0) {
            columnCountChanged = true;
            this.setColumnCount(3);     
        }
    }
}
blaind commented 10 years ago

This would be nice to get merged upstream.

TealOcean commented 10 years ago

+1 Same issue here.

NathofGod commented 10 years ago

I'm having this issue too, can't seem to solve it with the code above.

asunderland commented 10 years ago

I was having the same problem,code above didn't fix it,however if I set the column count to 1 in the xml it worked. Seems to need to remeasure itself.

dlangerenken commented 9 years ago

Thanks ptrent! I slightely changed it in my code as I'm too lazy to check whether I can call setColumns or not. So I "save" this value until the width is measured:

public class MyStaggeredGridView extends StaggeredGridView{

public MyStaggeredGridView(final Context context) {
    this(context, null);
}

public MyStaggeredGridView(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
}
public MyStaggeredGridView(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);
}

boolean columnCountChanged = false;
int columnCountPortrait = 1;
int columnCountLandscape = 1;

@Override
public void setColumnCountPortrait(int columnCountPortrait) {
    if (columnCountChanged) {
        super.setColumnCountPortrait(columnCountPortrait);
    }else{
        this.columnCountPortrait = columnCountPortrait;
    }
}

@Override
public void setColumnCountLandscape(int columnCountLandscape) {
    if (columnCountChanged) {
        super.setColumnCountLandscape(columnCountLandscape);
    }else{
        this.columnCountLandscape = columnCountLandscape;
    }
}

@Override
protected void onSizeChanged(int w, int h) {
    super.onSizeChanged(w, h);
    if(!columnCountChanged && getWidth() > 0) {
        columnCountChanged = true;
        this.setColumnCountLandscape(columnCountLandscape);
        this.setColumnCountPortrait(columnCountPortrait);
    }
}

}

jalopy8 commented 9 years ago

This is how I worked around the problem.

In StaggeredGridView,

// Modify existing setColumnCount()
public void setColumnCount(int columnCount) {
    setColumnCount(columnCount, true);
}

// Add new setColumnCount()
public void setColumnCount(int columnCount, boolean relayout) {
    mColumnCountPortrait = columnCount;
    mColumnCountLandscape = columnCount;
    if (relayout) {
        // mColumnCount set onSizeChanged();
        onSizeChanged(getWidth(), getHeight());
        requestLayoutChildren();
    }
}

In my code, call new setColumnCount() instead,

setColumnCount(2, false);

Hope that helps!

easycheese commented 9 years ago

Thanks @Dalanie your code helped me out :) Seems like this product isn't being supported anymore?...

isuruch commented 9 years ago

Thank you @jalopy8 Your method works like a charm...thank you again :+1: