Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
Android RecyclerView Load More on Scroll with Example
Next article icon

How to Disable RecyclerView Scrolling in Android?

Last Updated : 29 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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      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.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.


Next Article
Android RecyclerView Load More on Scroll with Example
author
aashaypawar
Improve
Article Tags :
  • Kotlin
  • Android

Similar Reads

  • How to Disable GridView Scrolling in Android?
    A GridView is a ViewGroup that can display data from a list of objects or databases in a grid-like structure consisting of rows and columns. Grid view requires an adapter to fetch data from the resources. This view can be scrolled both horizontally and vertically. The scrolling ability of the GridVi
    4 min read
  • How to Add Dividers in Android RecyclerView?
    In the article Android RecyclerView in Kotlin, it's been demonstrated how to implement the Recycler View in Android. But in the case of User Experience, the items need to be distinguished with the divider and proper padding and margins in each item. In this case, the Recycler View Item Decoration co
    9 min read
  • How to Improve RecyclerView Scrolling Performance in Android?
    RecyclerView is the main UI component in Android which is used to represent the huge list of data. If the RecyclerView is not implemented properly then it will not smoothly scrollable and it may lead to a bad user experience. To make it scrollable smoothly we have to optimize it and follow some tips
    4 min read
  • How to Animate RecyclerView Items in Android?
    RecyclerView Item animation is one of the modern features that we can add to our Android app, the basic working of this is when any user opens our app then the data items that are present in recycler view will animate and then it will show to the user.it is so easy to implement also it can enhance t
    5 min read
  • Android RecyclerView Load More on Scroll with Example
    Many apps display so many amounts of data in the form of a list. This data is so much so that it cannot be loaded at a time. If we load this data at a time then it may take so much loading time and degrades the performance of our RecyclerView. So to solve this we generally load the data in chunks an
    8 min read
  • CardView using RecyclerView in Android with Example
    RecyclerView is an extended version of ListView and GridView. It works on the ViewHolder design pattern. With the help of RecyclerView, we can add many extra features to our list of data. Before starting our example on the implementation of CardView in RecyclerView. We should know what CardView and
    9 min read
  • How to Add Fade and Shrink Animation in RecyclerView in Android?
    In this article, we are going to show the fade and shrink animation in RecyclerView. When we move downward then the item at the top will be fading out and then it will shrink. In the output, we can see how it is happening. We will be implementing Java/Kotlin programming language. Step by Step Implem
    15+ min read
  • Expandable RecyclerView in Android with Kotlin
    In this article, we will get to know how to implement the expandable RecyclerView. In General, we will show the list of items in the listview but in some situations like if you want to show the sub-list items, then we have to use an expandable list. See the below image for a better understanding. St
    7 min read
  • Android | Horizontal RecyclerView with Examples
    Recycler View is a ViewGroup added to Android Studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item which can be customiz
    4 min read
  • How to Use SnapHelper in RecyclerView in Android?
    SnapHelper is an amazing feature that is seen in RecyclerView. With the help of this feature, we can make the items of the RecyclerView properly visible. This feature of Recycler View is present in most of the apps, but it is not visible. This feature is generally seen in the Google Play application
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences