RecyclerView in Fragment in Android Studio using Java | YouTube Clone

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign welcome back to my channel in this video we will be creating recycler view inside the fragment I have already created a video of recyclerview inside an activity so the process is quite similar but still beginners are kind of confused hence we will clear all this confusion in this video so this project is a continuation of my previous video where I have created navigation drawer and bottom navigation where you can switch from one fragment to another fragment in bottom navigation as well as a navigation drawer you can use your own fragment project as well that is also fine so we will be creating a recycler view in home fragment like this way this is a quick little preview on how our project will look by the end of this video it's kind of like a clone of YouTube home page right where we have a thumbnail title and channel details so yeah with that said let's get started with our project first go to colors.xml here we already have black white and red color so we will add another color for that press Ctrl D to duplicate the line then name it as Gray and hex value as D3 D3 D3 okay then we need to add six drawables here we already have previously added Vector assets now we will add three thumbnails and three Channel logo okay so let me quickly copy paste all the drawables and it's done as you can see we have successfully added six drawables now all the prerequisites are done let's move on to create recycler view in our home fragment this is our home fragment so let me remove all the unnecessary code we will be using frame layout which will help the fragments to replace each other seamlessly so add recycler View keep both of them as smash parent and then give the ideas home recycler View as we have used custom toolbar and bottom navigation bar so we need to make sure that we leave that space otherwise it will overlap each other so for that I will give top margin as 60 DP and then bottom margin also has 60 dip now as we are creating recycler view so obviously we will need a recycler view item as well right so right click on layout folder then new and then layout resource file write the file name as item underscore video then keep the root element as linear layout and then click ok here we'll be creating two image View and two text view image view will be for thumbnail and Channel logo and text view will be for video title and channel name got it so make sure linear layout parent orientation is vertical then first we will create thumbnail so for that we will use image View keep the height as 200 DP and ID adds video thumbnail then add all the other attributes as well now we will create horizontal linear layout and then inside that we will create a channel image so for that create an image View keep the size as 40 DP and ID as Channel image then add all the other attribute now we will again create a linear layout but keep the orientation as vertical this time then inside that we will create a text View which will be our video title so add all the necessary attributes I know text is not visible here because text color is white but it will be visible on fragment because the fragment background color is black okay then we will copy paste the text view that will be our channel name so change the text color as Gray then all the attributes as well and it's done as we are creating recycler view so we will require data class as well as adapter class so first let me create a data class so right click on package name then new and then Java class I'll name it as video item here we will add all the items that are present in recycler View so let me quickly create all the variables video thumbnails video title channel name and channel image now we will require a Constructor so right click then go to generate and choose Constructor select all the variables as well as class and then click ok and see our Constructor is ready now as we need to access those data so for that we will require getter so right click then go to generate and choose getter select all the variables and click ok and see we have all the getter for our variables perfect so we are done with our video item now it's time to create adapter so adapter always act as a bridge between UI and data okay so right click on package then new and then Java class I limit as video adapter now as we are creating recycler view so extends recycler View dot adapter inside that adapter we will have video adapter Dot video view holder which we have not created so let's create a video view holder class that extends recycler View Dot View holder so let me quickly create all the four variables [Music] and done now click on implement superconstructor and it will automatically going to create it for you here we will attach all the variables with their respective item so let me quickly do that and it's done now let me create a list inside that we will have the video item class and store it in a video list variable now we need to create a Constructor so right click go to generate and then Constructor select variable as well as class and then click ok and see Constructor is automatically created now click on Implement methods and select all the methods and then click ok so we have three methods on create view holder on bind view holder and get item count so first let us write code for oncreate view holder so here we will attach item video XML with a recycler view so for that create View and with the help of layout inflator inflate the item video layout then return item View next we will call video item class and store video list position in that then with the help of holder we will set the resources which we will get them through the getter that we have created in video item class right and similarly we will create for all the other variables as well video title channel name and channel limit [Music] foreign [Music] [Music] count so it will return the video less slides and that's it now we will connect everything in home fragment this is how default fragment will look alike so let me quickly remove all the unnecessary code only this three should be present so first let me create all the variables and done keep the empty Constructor as it is then move on to create a new method called as generate video item here we will create a new array list that will be stored in video items variable then add recycler view items inside the video items arraylist so make sure that you follow the exact position for each item so here first we have thumbnail then video title then channel name so in the channel name I have added views and time as well and then last we have Channel image so I'll add the respective drawable similarly let me create two more item [Music] [Music] thank you foreign then return the video items now inside the oncreate view first store the layout in root view variable and then store generate video item method in video list now we need to set up a recycler view so initialize it first then set layout as linear layout manager then we need to store video list in the video adapter and then set the video adapter on the recycler View lastly return the root View and that's it now let's run the app all right we have done a minor mistake over here as you can see only one recycler view item is visible but we have created three items right so here one item is taking an entire screen because go to item video dot XML and here we have set both of them as match parent that is incorrect so instead send the height as wrap content Okay now click on apply change and see we have three items in our recycler view perfect it is kind of lagging but it will work seamlessly in your physical device okay also we can switch from one fragment to another fragment here we have a recycler view item with video thumbnail Channel logo video title and also the channel name with views and time perfect 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] foreign
Info
Channel: Android Knowledge
Views: 8,545
Rating: undefined out of 5
Keywords: recyclerview fragment, recyclerview in fragment, recyclerview inside fragment, android recyclerview in fragment, recyclerview in fragment android studio, recyclerview, recyclerview tutorial, recyclerview kotlin, recyclerview android example, recyclerview android, recyclerview in fragment android studio java, recyclerview in fragment android tutorial, how to implement recyclerview in fragment, recyclerview adapter, recyclerview in fragment android studio kotlin, java, youtube clone
Id: R62YihuL4VI
Channel Id: undefined
Length: 21min 21sec (1281 seconds)
Published: Wed May 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.