androidgridviewandroid-arrayadapterfastscrollsectionindexer

Material fastscroll thumb with GridView and ArrayAdapter


I have a GridView layout that makes use of an ArrayAdapter to populate its contents. I want to make use of fast-scrolling and as such have added the following attributed to the layout XML:

android:fastScrollAlwaysVisible="true"        
android:fastScrollStyle="@android:style/Widget.Material.FastScroll"

I am now able to make use of fast scrolling to navigate but would now like to add a material thumb preview as such:

fastscroll thumb preview

From my understanding, I would have to implement the SectionIndexer interface from my ArrayAdapter as so:

class exampleArrayAdapter extends ArrayAdapter<...> implements SectionIndexer

At this point, I have reached a bump and can't figure out how to get the thumb preview and fear I may be doing something wrong. Pointers as to how I can get this working or what I should look up would be appreciated.


Solution

  • I have finally had time to look back at this, and the solution turns out to be very trivial! This is what I did:

    @Override
    public Object[] getSections() {
        ArrayList<String> labels = new ArrayList<>();
        for (LaunchableActivity activity: mActivityInfos) {
            labels.add(activity.getActivityLabel());
        }
    
        return labels.toArray();
    }
    
    @Override
    public int getPositionForSection(int i) {
        return i;
    }
    
    @Override
    public int getSectionForPosition(int i) {
        // We do not need this
        return 0;
    }
    

    I had a list of LaunchableActivity and based of that created a sections array to be returned. For my needs, all I required was to implement getPositionForSection and not getSectionForPosition. Your use case may vary.

    The source code where I implemented this is available here, specifically on commits: