Open In App

How to Add Fast Scroller in RecyclerView using Recycle-Fast-Scroll?

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to implement fast-scroll using the recycle-fast-scroll library. For the item, in the RecyclerView we are just implementing a button and the total number of buttons will be set by us in the MainActivity. The name of the button will be the position of the button. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language. 

Step by Step Implementation

Step 1: Create a new Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.  

Step 2: Add this into build.gradle:(app) file

implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.github.plusCubed:recycler-fast-scroll:0.3.2'
implementation 'com.google.android.material:material:1.0.0'

Add this into build.gradle:(project) file

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

Step 3: Working with the items.xml file

Go to the app > res > layout > New > Layout Resource File and name the file as item. Go to the item.xml file and refer to the following code. Below is the code for the item.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
 
    <Button
        android:id="@+id/itemclick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Here" />
 
</LinearLayout>


Step 4: Working with the RecyclerViewAdapter.java file

Create a new java class in android studio and name the class as RecyclerViewAdapter. Go to the RecyclerViewAdapter.java file and refer to the following code. Below is the code for the RecyclerViewAdapter.java file. 

Java




import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
 
    private int mcount;
 
    public RecyclerViewAdapter(int mcount) {
        this.mcount = mcount;
    }
 
    @NonNull
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
        return new ViewHolder(view);
    }
 
    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
        // Adding the items in recycler view with name "Button" + position
        holder.itemclick.setText("Button" + position);
    }
 
    @Override
    public int getItemCount() {
        return mcount;
    }
 
    public class ViewHolder extends RecyclerView.ViewHolder {
        Button itemclick;
 
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemclick = itemView.findViewById(R.id.itemclick);
        }
    }
}


Step 5: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rcv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
    <com.pluscubed.recyclerfastscroll.RecyclerFastScroller
        android:id="@+id/fasttrcv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        app:rfs_barColor="#07adf4"
        app:rfs_handlePressedColor="#df56C6" />
     
</RelativeLayout>


Step 6: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import com.pluscubed.recyclerfastscroll.RecyclerFastScroller;
 
public class MainActivity extends AppCompatActivity {
 
    RecyclerView recyclerView;
    RecyclerFastScroller fastScroller;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        // initialising the recycler view
        recyclerView = findViewById(R.id.rcv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
         
        // Initialising the size of recycler as 50
        recyclerView.setAdapter(new RecyclerViewAdapter(50));
        fastScroller = findViewById(R.id.fasttrcv);
        fastScroller.attachRecyclerView(recyclerView);
    }
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads