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

The problem of top margin #183

Open kyungin-park opened 9 years ago

kyungin-park commented 9 years ago

I had a problem of "top margin". The margin of column[0] was lower than column[1] for the first time.

I finally found what is the cause of the problem and I am sharing with you guys to prevent having the same problem.

On StaggeredGridView.java file,

//////////// I added two functions below.
private boolean getPositionIsHeaderFooter(final int position) {

    GridItemRecord rec = mPositionData.get(position, null);
    return rec != null ? rec.isHeaderFooter : false;
}

private void removePositionColumn(final int position) {
    GridItemRecord rec = mPositionData.get(position, null);
    if (rec != null) {
        mPositionData.remove(position);
    }
}

And then see if data is not matched to HeaderOrFooter.

@Override
protected void onChildCreated(final int position, final boolean flowDown) {
    super.onChildCreated(position, flowDown);
    if (!isHeaderOrFooter(position)) {

       ///////////// I added a if statement below.
   // remove data which is unmatched to HeaderFooter or General one.
        if(getPositionIsHeaderFooter(position)) {
            removePositionColumn(position);
        }

        // do we already have a column for this position?
        final int column = getChildColumn(position, flowDown);