Recyclerview in Fragment Android Studio Tutorial || Recyclerview || Fragment || Kotlin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there everyone welcome to another video fox and white so in this video we're gonna learn how to implement a recycler view inside of fragment right i have already made tutorials in which i have implemented recyclerview inside an activity so many of you were commenting to make a tutorial in which we implemented a cycler view inside a fragment so this tutorial is for those people who want to learn that so let me just give you a glimpse of what we're going to get by the end of this video so i just open up my emulator here you can see this is a recycler view right we have some list items these are hard-coded list items but this recycler view is not implemented inside an activity i mean the cycloview is not a direct child of this activity it is a child of a fragment inside an activity right so this is what we're gonna learn and we get by the end of this tutorial so if you want to learn the same and implement it to your android application make sure you watch this video till the end so without much ado let's get started [Music] so guys if you're in this channel make sure to hit that subscribe button press the bell notification icon for seeing notification of the upcoming videos and by the end of the video if you liked the video make sure to hit that thumbs up button as well so guys in this tutorial we'll be using some uh constant data for example these images and if i just open up my string xml this hardcoder strings will be using so some styles are there so i do not want to waste my time on this so those of you who want to make the same project that i'll be teaching in this tutorial for you guys i have made a base project right in which all these things will be there right you go to this link i'll put the link for this in the description you go here and you just clone this project right and you open up this project the all these things will be done in this project all the strings and these images will be there so we do not need to waste time on this right so i hope that you guys have pause the video and clone this project so the very first first thing we are going to do is we are going to go to our main activity and we will implement a frame layout here right and we'll align it or i would say constraint it to the parent on all the post sites will give it id frame layout and now we need a fragment in which we are going to implement our recyclerview so let us just add an empty fragment so fragment blank fragment and let me just give it a name of fragment so okay we have got the fragment let us store the front end code delete this and now we are going to add a recyclerview inside this it's okay give it id i'll just give it id of recyclerview only and for the list item we already have the list item in our project we have made this list item this was already designed in your base project so we have added this so this is how our cycler view will look like with all the items in it and now so that is pretty much everything for the front end part so now let us just dive into the back end code the main focus of this video so the very first thing we need is we need a data class so just right click here so we need need a data class and we are going to name it as news because it will define the object of our news item this list view right so our item has two main things the image and the heading so you can modify it according to your items now this class will represent our object of our cycle review item so we have created a data class now we need the adapter for a cycler view so just go here cartoon class and we are going to name it as my adapter now this will take the object or you would say the list the news items list now we need to define an inner class so this is raptor will be exactly same the way we are going to implement it inside an activity so we need to implement some members so just select all okay and let me just cut this done press alt enter to implement this class and here we forgot to put this right so this is will be our view holder and these three methods we need to overwrite so let us just override the very first method on bind view holder well [Music] current item is equal to the item of the news list and we'll get the position here folder dot okay we have not implemented this as well so let us just do this real quick we will set the title image of our list item right so we'll get the current item and we'll set the title image and the text or heading of that particular item with the help of this list that will get even argument so now let us just override this oncreateviewholder done and the last thing we need to override is this get item count so we'll just return the number of items that are there in the news list return useless. so that is pretty much everything we need to do inside this adapter now let us just go to our segment now we need to declare certain reference variables let me just quickly do that so okay we have defined the reference variable for the adapter recyclerview and we have defined an arraylist which will contain the new subjects then we have created three arrays one for the image id heading and the news rate so let's just go here and create a method for initializing the data [Music] so we have initialized the array list for the news so these this array will contain the image ids right and then we have the array for the heading so if you guys are wondering i'm using ctrl d to duplicate each line i know okay guys so inside this method what we have done is we have initialized our arraylist that will contain the object of the news then we have initialized all the three arrays for the image id the heading and the news itself right then we have created the object of the news for all the items that are present inside this image id right and we have added the image id and the heading you must you guys must wonder that why we have added this so in case you want to implement a on click listener and you want to show the whole image so for that i have implemented this in this tutorial will not be using this particular array so you can skip this part so yeah so we have created all the objects and we have added all the objects to the news array list we are going to call this method and for that we need to write [Music] on view created right so we want to write inside this method so first we are going to initialize the data so okay we have called the data initialize that will initialize all the data the hardcore data that we have there in the project then we have uh set the parameters to the view layer layout manager linear layout manager then we have referred to the recyclerview with the help of find viewpid then we have set this layout manager to our recyclerview said you said the fixed size is true then we have created the object of the adapter that we have created here right we have created the object of this and we have passed this new surrey list as an argument because here we have defined that whenever the object of this class will be created we'll get a arraylist that will contain the news objects right it's okay then we have set the adapter to our recyclerview so that is pretty much everything we need to do inside our fragment so the last thing is that we want to replace our frame layout with our fragment so let us quickly do that so we have already done this in many of the videos so we'll create a method called replace fragment will pass the object of the fragment that we want to replace it with right we want to replace the frame layout with our home fragment so let us just create this method an so guys this line of code will replace our frame layout with the fragment that will get it here as an argument so we have done everything that needs to be done to implement the recyclerview insider fragment now let us just quickly install this app to our emulator and see if it is working fine or not right so case the app has been installed to our emulator and here you can see we can see all the items inside the cycler view and this recycler is implemented inside the fragment so this is how you implement the cycle of insider fragment so i hope that you guys have understood the code but still if you have any doubt you can always ask me in the comment section i can dm me on instagram the instagram username will be there on screen right now and if you like the video make sure to hit that thumbs up button in case you know this channel make sure to add the subscribe button and press the bell notification icon for seeing notification of the upcoming videos so that is it for today's video see another video bye [Music] you
Info
Channel: Foxandroid
Views: 35,632
Rating: undefined out of 5
Keywords: recyclerview in fragment, recyclerview in android, recyclerview, fragment, android studio, recyclerview inside fragment, kotlin, recyclerview adapter, 2022, foxandroid, androiod fragment recyclerview, how to implement recyclerview in fragment, recyclerview fragment, fragment recyclerview, custom recyclerview, recyclerview kotlin, recyclerview tutorial, recyclerview in fragment android tutorial, android recyclerview in fragment, recyclerview in fragment android studio kotlin
Id: 5mdV1hLbXzo
Channel Id: undefined
Length: 14min 45sec (885 seconds)
Published: Sun Jul 03 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.