How to Disable RecyclerView Scrolling in Android?

29 Jul, 2021
RecyclerView is a view group used for displaying data from arrays and databases. RecyclerView basically is a list of items from the data. RecyclerView is often referred to as a successor of GridView and ListView. More about RecyclerView could be found at RecyclerView in Android with Example. RecyclerView lets the users scroll up and down and left and right by setting appropriate orientation via attributes. Most of the applications that we use today prominently use RecyclerView to display or present the data. 
RecyclerView examples
Through this article, we want to show you how you could disable the scrolling ability of the RecyclerView in Android.
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: 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. Create this simple RecyclerView in the layout.
XML
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <androidx.recyclerview.widget.RecyclerView        android:id="@+id/recycler_view_1"        android:layout_width="match_parent"        android:layout_height="match_parent"/>  </androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create a Card for the RecyclerView (card.xml)
We need to create a layout for displaying our data. In our case, we have a list of cities. So each of such cards will display the city name in the TextView.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content">      <TextView        android:id="@+id/place_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:layout_marginVertical="30sp"        android:textSize="70sp"/>  </RelativeLayout>
Step 4: Create an Adapter for the RecyclerView (MyRecyclerViewAdapter.kt)
We have to create an adapter to pass data (array of the city names) to the RecyclerView.
Kotlin
import android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport android.widget.TextViewimport androidx.recyclerview.widget.RecyclerView  private val myItemList = arrayListOf("Delhi", "Mumbai", "Hyderabad", "Bangalore", "Chennai", "Kolkata")  class MyRecyclerViewAdapter: RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>() {      inner class ViewHolder(v: View): RecyclerView.ViewHolder(v), View.OnClickListener{        val tvPlaceName: TextView = v.findViewById(R.id.place_name)        override fun onClick(v: View?) {            TODO()        }    }      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyRecyclerViewAdapter.ViewHolder {        return ViewHolder(LayoutInflater.from(parent.context)            .inflate(R.layout.card, parent, false))    }      override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {        holder.tvPlaceName.text = myItemList[position]    }      override fun getItemCount(): Int {        return myItemList.size    }}
Step 5: Link the RecyclerView and the Adapter in the Main code (MainActivity.kt)
Refer to the comments inside the code.
Kotlin
import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport androidx.recyclerview.widget.LinearLayoutManagerimport androidx.recyclerview.widget.RecyclerView  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Declaring the recycler view from the layout file        val myRecyclerView = findViewById<RecyclerView>(R.id.recycler_view_1)          // Declaring a variable for         // Initializing Linear Layout Manager        val myLinearLayoutManager = LinearLayoutManager(this)          // Setting the layout manager of the        // recycler view with the Initialized variable        myRecyclerView.layoutManager = myLinearLayoutManager          // Setting the adapter of the recycler view        // with the adapter we created        myRecyclerView.adapter = MyRecyclerViewAdapter()      }}
Output: Run the application
You can see that we are able to scroll.
Step 6: Edit the layout manager to disable the RecyclerView scrolling
Kotlin
import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport androidx.recyclerview.widget.LinearLayoutManagerimport androidx.recyclerview.widget.RecyclerView  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          val myRecyclerView = findViewById<RecyclerView>(R.id.recycler_view_1)          // Calling the override functions from         // the Linear Layout Manager inner class        val myLinearLayoutManager = object : LinearLayoutManager(this) {            override fun canScrollVertically(): Boolean {                return false            }        }        myRecyclerView.layoutManager = myLinearLayoutManager        myRecyclerView.adapter = MyRecyclerViewAdapter()    }}
Output: Now run the application
Now, you can see that we are unable to scroll.
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Android SDK and it's Components
Flutter - Custom Bottom Navigation Bar
How to Add Views Dynamically and Store Data in Arraylist in Android?
Retrofit with Kotlin Coroutine in Android
How to Post Data to API using Retrofit in Android?
Android UI Layouts
Kotlin Array
How to Add Views Dynamically and Store Data in Arraylist in Android?
Retrofit with Kotlin Coroutine in Android