How to Disable RecyclerView Scrolling in Android?
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 by Step Implementation
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 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 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.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import 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.AppCompatActivity import android.os.Bundle import androidx.recyclerview.widget.LinearLayoutManager import 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.AppCompatActivity import android.os.Bundle import androidx.recyclerview.widget.LinearLayoutManager import 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.
Please Login to comment...