RecyclerView in Android Studio using Kotlin | Android Knowledge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign welcome back to my channel in this video we will be creating a recycler View now a lot of people have confusion with list View and recycler View and trust me this is one of the most asked question in interview what is the difference between list View and recyclery see in list view we simply add items in a list format correct but in recycler view every item is replaced or recycled with each other as we scroll down hence the name as recyclerview so this was the major difference between list View and recycler view cut it now let's get started with the project name the activity as recyclerview kotlin make sure the language is totally and then click on finish first let's do the prerequisite part so go to colors.xml add a new color as lavender then hex code AS 8692 F7 and then now go to themes.xml change the color primary as lavender and also color primary variant as lavender now go to strings.xml change the app name as Android basic tutorials or maybe to do list whatever you want to keep now we will add all the images in the drawable so copy all the images and paste it in the drawable and we are done with the prerequisites now close all the tabs go to activity main.xml here we will add the recycler View so make sure width and height is match parent then keep the IDS recycler View and then margin top as 12 DP that's it now as we are creating recycler view so we will require a specific item layout right so right click on layout then new and then layout resource file name the activity as item layout and click ok now here we will design a very simple layout in which we will have one image View and one text view cut it so instead of constraint layout I'll use card View make sure you keep the height as wrap content and add all the required attributes now inside the card view we will create a constraint layout and inside that constraint layout we will create our image View keep the width and height as 60 DP then IDs image and then add all the required attributes and done now create a text View keep the width and height as wrap content then IDs title and then add all the required attributes and done now as we are creating recycler view we will require a data class as well as adapter class so first let's create data class right click on package name then new and then kotlin class choose data class and name it as data class so we have two elements right one is image View and second is text view correct so all you have to do is declare both the variables over here now let's create the adapter class so right click on package name then new and then code link class name it as adapter class first let me write the code and then I'll explain you see first we have created an arraylist of our data class which we created before right in which we have stored image View and text view correct all of these things will be accessed by a variable called as data list got it then in Java we used to write extends right but in kotlin you just have to use colon recyclerview dot adapter inside that we have declared adapter class Dot View holder class we haven't created it yet so let's create it first click on create view holder so basically in view holder we attach all the elements with the respective variables okay now extends recyclerview.viewholder and inside that it will have item view which is basically our view so out of both the elements first was image view I'll initialize it and store it in the RV image variable and then the same way I'll do it for text view as well and store it in the RB title variable and then now we need to create three methods of recycler view so click on red bulb and then click on Implement members then select all the three members and click ok we will shift The View holder class at the bottom then inside the oncreate view holder we will initialize our item layout so I will store everything inside the item view variable and then using layout inflator inflator item layout.xml then return the item View now inside the onbind view holder we usually attach the elements variable with the respective data variables first I will store the position which we get from data list in the current item variable then with the help of holder we will set RB image using set image results to its current item of data image and same way we will do it for RP title as well but here instead of set image resource we will use text and done then the last method that is get item count here we will set the size of data list which is nothing but a data class arraylist and that's it now let's move to our main activity declare all the necessary variables now we will create a get data method so private fun that is function then get data first let me write the code and then I'll explain you foreign [Music] Loop then inside that for Loop we will store images dot indices which means the index value of image list so basically in image list we will store all the images in an array format then the for Loop will run until it reaches the index value which means the end of an arraylist got it and then we have stored the index value in the I variable now we have declared a variable called as data class in which we have called the actual data class and inside that mention the parameters make sure that you mention the parameters in their exact position like the way you call them in data class so for like example we have called image first and then title in the data class right so the same way over here we have first called the image list and then title list got it both of them have I which means the index value right then we will add all of this thing in the data list so the data list is nothing but a arraylist of data class only got it then set the adapter on the recycler View and that's it we have already declared image list and title list but now let's initialize it so first create an array of image list and inside that array include all the drawables so let me quickly do that and done then the same way I create an array of title list and inside the array include all the string so let me quickly do that and it's done now lastly we need to set up the recyclerview layout so first initialize it then with the help of layout manager set it as linear layout manager and set has fixed size as 2 which means the recyclerview items width and height will be seen for every item cut it then set an array lives that contains the object of data class and store it in the data list so let me quickly tell you what is the difference between an array and array list so array stores fixed size of elements which is of same data type while an arraylist stores Dynamic Elements which can be of different data types okay then lastly call the get data method and we are done with the coding part now let's run the app see we have a recycler view perfect you can make all the items clickable by using on item click listener I have already created a video on it so you can click on the I button to watch so yeah that is it for the video and if you are new to this Channel please consider subscribing to my channel and I'll see you in the next video [Music]
Info
Channel: Android Knowledge
Views: 9,017
Rating: undefined out of 5
Keywords: recyclerview android, recyclerview androidx, recyclerview kotlin, recyclerview androidx dependency, recyclerview kotlin 2021, kotlin recyclerview android, recyclerview kotlin foxandroid, recyclerview explained, recyclerview example, recyclerview tutorial, recyclerview, recyclerview with images and text, kotlin recyclerview adapter, recyclerview clip to padding, android recyclerview cardview, kotlin recyclerview example, recyclerview with images, android recyclerview example
Id: UDfyZLWyyVM
Channel Id: undefined
Length: 12min 11sec (731 seconds)
Published: Mon Jun 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.