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

ArrayIndexOutOfBoundsException #104

Open blundell opened 10 years ago

blundell commented 10 years ago

Maybe linked to #62

I think we've got the same.

05-23 15:02:12.962  28126-28126/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.blundell.app, PID: 28126
    java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
            at com.etsy.android.grid.StaggeredGridView.getChildTop(StaggeredGridView.java:572)
            at com.etsy.android.grid.ExtendableListView.fillGap(ExtendableListView.java:1259)
            at com.etsy.android.grid.ExtendableListView.moveTheChildren(ExtendableListView.java:1233)
            at com.etsy.android.grid.ExtendableListView.scrollIfNeeded(ExtendableListView.java:1059)
            at com.etsy.android.grid.ExtendableListView.onTouchMove(ExtendableListView.java:880)
            at com.etsy.android.grid.ExtendableListView.onTouchEvent(ExtendableListView.java:698)
            at android.view.View.dispatchTouchEvent(View.java:7714)

Reproduction steps:

From this screenshot i think it shows, the mColumnBottoms knows it has 1 column but the mPositionData has not updated and still has the old (landscape) 2 column data. Therefore when getPositionColumn(int position) is called which references mPositionData it can return a false column number. Leading to the ArrayIndexOutOfBoundsException in getChildTop. i.e. position 5 has column 1 when it should have column 0 (portrait == 1 column == mColumnBottoms.length) screen shot 2014-05-23 at 15 42 04

sujitreddyg commented 10 years ago

Try making the changes below to onRestoreInstanceState() method in StaggeredGridView.java, hope this helps.

@Override
public void onRestoreInstanceState(Parcelable state) {
    GridListSavedState ss = (GridListSavedState) state;
    mColumnCount = ss.columnCount;
    mNeedSync = true;

    boolean isLandscape = false;
    int orientation = getContext().getResources().getConfiguration().orientation;
    if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
        isLandscape = true;
    } else {
        isLandscape = false;
    }
    int newColCount = isLandscape ? mColumnCountLandscape : mColumnCountPortrait;

    if(newColCount == mColumnCount) {
        mColumnTops = ss.columnTops;
        mColumnBottoms = new int[mColumnCount];
        mPositionData = ss.positionData;
    }
    mColumnCount = newColCount;
    super.onRestoreInstanceState(ss);
}