ShamylZakariya / StickyHeaders

Adapter and LayoutManager for Android RecyclerView which enables sticky header positioning.
MIT License
1.4k stars 185 forks source link

IndexOutOfBoundsException: onBindItemViewHolder #23

Open rusmichal opened 8 years ago

rusmichal commented 8 years ago

I found one issue

@Override
    public void onBindItemViewHolder(ItemViewHolder viewHolder, int sectionIndex, int itemIndex, int itemType) {
        int sectionPosition = includeHeader ? sectionIndex - 1 : sectionIndex;
        switch (itemType) {
            case ITEM_HEADER:
               ...
                break;
            case ITEM_TILE:
                initTile((FeedViewHolder) viewHolder, feedDataProviders.get(sectionPosition).getTileViewModelList().get(itemIndex), sectionPosition);
                break;
            case ITEM_FOOTER:
                ...
                break;
            default:
                break;
        }
    }

Where feedDataProviders is List<FeedDataProvider>. FeedDataProvider has list of custom objects TileViewModel.

I got IndexOutOfBoundsException: Invalid index 0, size is 0 in line initTile() because getTileViewModelList() return empty list and itemIndex is 0. I think if getNumberOfItemsInSection return 0 then is should not called the onBindItemViewHolder right?

@Override
    public int getNumberOfItemsInSection(int sectionIndex) {
        if((includeHeader && sectionIndex == 0) || (includeFooter && sectionIndex == getNumberOfSections() - 1)) {
            return 1;
        }
        return feedDataProviders.get(includeHeader ? sectionIndex - 1 : sectionIndex).getTileViewModelList().size();
    }
ShamylZakariya commented 8 years ago

Good point. I'll look into fixing this. Thanks!