Open sin3hz opened 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);
}
}
}
This would be nice to get merged upstream.
+1 Same issue here.
I'm having this issue too, can't seem to solve it with the code above.
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.
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);
}
}
}
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!
Thanks @Dalanie your code helped me out :) Seems like this product isn't being supported anymore?...
Thank you @jalopy8 Your method works like a charm...thank you again :+1:
Hi, when I add setColumnCount(3) follow
It just show one column, and the item overlay each other.see the screenshot.![screenshot_2014-03-18-10-10-15](https://f.cloud.github.com/assets/5888216/2443868/98faf3fc-ae42-11e3-9239-bfa938f464ba.png)