How to Disable RecyclerView Scrolling in Android?
Last Updated : 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 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.
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