Adnan Hakim
Adnan Hakim

Reputation: 65

Recyclerview Alphabetical Scrollbar

I need to implement a recyclerview alphabetical scrollbar similar to Samsung Music app (I can't post the image because of low reputation)

I have read all posts regarding this, but I don't want the bubble scroll. I have all the alphabets in a vertical LinearLayout, and I want to know how to scroll to the particular item.

Upvotes: 3

Views: 4414

Answers (1)

sanoJ
sanoJ

Reputation: 3128

You can use this library. To get more information checkout the README

enter image description here

Step 1: add the dependecies

In build.gradle (Project)

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

In app gradle

dependencies {
    // AppCompat version
       implementation 'com.github.myinnos:AlphabetIndex-Fast-Scroll-RecyclerView:1.0.92'
    or
    // AndroidX version
       implementation 'com.github.myinnos:AlphabetIndex-Fast-Scroll-RecyclerView:1.0.94'
}

Step 2: Add the layout

<in.myinnos.alphabetsindexfastscrollrecycler.IndexFastScrollRecyclerView
    android:id="@+id/fast_scroller_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Step 2: implement SectionIndexer to RecyclerViewAdapter.

public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> implements SectionIndexer {

private List<String> mDataArray;
private ArrayList<Integer> mSectionPositions;

.....

@Override
public int getSectionForPosition(int position) {
    return 0;
}

@Override
public Object[] getSections() {
    List<String> sections = new ArrayList<>(26);
    mSectionPositions = new ArrayList<>(26);
    for (int i = 0, size = mDataArray.size(); i < size; i++) {
        String section = String.valueOf(mDataArray.get(i).charAt(0)).toUpperCase();
        if (!sections.contains(section)) {
            sections.add(section);
            mSectionPositions.add(i);
        }
    }
    return sections.toArray(new String[0]);
}

@Override
public int getPositionForSection(int sectionIndex) {
    return mSectionPositions.get(sectionIndex);
}

}

If it's hard to figure out thing, have a look at the sample app

Upvotes: 2

Related Questions