danoz73 / RecyclerViewFastScroller

A Fast Scroller for the RecyclerView world!
Other
1.13k stars 211 forks source link

ArrayIndexOutOfBoundsException #58

Open shayanpourvatan opened 8 years ago

shayanpourvatan commented 8 years ago

i've got an indexOutOfBound in AbsRecyclerViewFastScroller$override.updateSectionIndicator, after tracing your code, i've seen position that came to the updateSectionIndicator is bigger than size of my list, i've add following part of code :

 if (section < 0 || section >= sections.length) {
       return;
 }

so updateSectionIndicator will be :

private void updateSectionIndicator(int position, float scrollProgress) {
        if (mSectionIndicator != null) {
            mSectionIndicator.setProgress(scrollProgress);
            if (mRecyclerView.getAdapter() instanceof SectionIndexer) {
                SectionIndexer indexer = ((SectionIndexer) mRecyclerView.getAdapter());
                int section = indexer.getSectionForPosition(position);
                Object[] sections = indexer.getSections();

                if (section < 0 || section >= sections.length) {
                    return;
                }

                mSectionIndicator.setSection(sections[section]);
            }
        }
    }

error fixed with this code, please fix this in next update. thanks for your great library.

shayanpourvatan commented 8 years ago

better way is adding following part to scrollTo :

  if (position >= mRecyclerView.getAdapter().getItemCount()) {
            position = mRecyclerView.getAdapter().getItemCount() - 1;
  }

so result will be :

    @Override
    public void scrollTo(float scrollProgress, boolean fromTouch) {
        int position = getPositionFromScrollProgress(scrollProgress);

        if (position >= mRecyclerView.getAdapter().getItemCount()) {
            position = mRecyclerView.getAdapter().getItemCount() - 1;
        }

        mRecyclerView.scrollToPosition(position);

        updateSectionIndicator(position, scrollProgress);
    }

or changing getPositionFromScrollProgress to :

 private int getPositionFromScrollProgress(float scrollProgress) {
        return (int) ((mRecyclerView.getAdapter().getItemCount() - 1) * scrollProgress);
    }

( add (-1) to calculation ). I don't know witch one is better but all of them fixed my problem