Android Jetpack Paging Version 3.0 | Kotlin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to my channel in today's video i'm going to show you how you can use jetpack paging version 3 library also i'm going to use retrofit and glide as well so let's begin with adding the dependencies as we do it at the very beginning so first of all i am adding the retrofit dependency we are going to use retrofit 2. as we know with the retrofit we need a json converter so we'll add that dependency also i'm going to use glite library so i'm adding glide now along with that we are going to use the mvvm so i am adding the viewmodel dependencies i am adding view model ktx as well which is going to use for the paging and this is the last dependency which we are going to use for the paging this is the beta version currently so we are going to use the beta version as this is our main activity so first of all we need to create a network package or there will create some classes like the retro instance and this retro instance is going to return us the instance of retrofit librarians which is going to be inside the companion object block and this get retro instruments function will return the instance of retrofit before we implement this function we need a url so i am going to use a url which is rican which is going to rick and morty api which basically give us some characters information so i'm going to use that api here we'll add the json converter factory that's it for our retro instance class inside this we need a interface that is going to be a retro service and this retro service is going to hold a get method type of a function that is going to call from the viewmodel that is going to be the suspend function as it is going to call from from the uh routine so this version three library support the core routine as well and we're going to pass page number as the query parameter and this is going to return our data so let's create our data class i'm giving it as the same name rick and morty list this is going to be the data class and let's see how api look like so i'm formatting it this is the api data we are going to receive so we'll having the info and the result so let's create the info class first and which is going to have this parameter page is next previous count will keep the same name what is returned by the api now i'm going to keep them as the nullable because sometimes next and previous may be the null also second is the result so that is the array then we'll keep we'll use the name and spaces and the image so there's three parameters we need for for our recycler view so i'm just going to parse three values if you need more then you can pass inside this character data class and that will give you the access of those values so i'm just keeping three parameters currently and we'll pass it here list and type of character data results is the same name results now retro service will going to return this data class so retro service classes interface is also ready now so let's go to main activity and work on our layout file so we are going to use a recycler view here so i'm simply adding a recycler view giving its id and we don't need text and we need to keep it on the top now we'll create a separate function for initializing our recycler view so i can't access recyclerview directly so we need to add kotlin android extension so when we add and sync our gradle file at that time we should be able to access our layout views directly from the activity or the fragment so now you see i can access recyclerview directly instead of calling find vui id when inside the view block we are going to set the layout manager currently i'm going to keep it as the linear layout manager we need a separator so i'm adding a separator now now we'll set our adapter so currently we don't have any adapter so let's create it we'll create a separate class for recyclerview adapter so i'm giving it as the same name recyclery adapter and it's not going to be the regular adapter for us it's going to be a little different because we are going to implement paging here so it's going to be extend basing data adapter i'm going to have the data type as the character data and will pass the video folder now we'll implement both the method and we'll create our view holder class when inside it a bind function now we'll inflate our layout okay we are doing inside the wrong function so this in in fact we need to do it inside the oncreateviewholder function instead of on bind view holder so i was doing it in the wrong function so now we need to create our layout file for our recycler view i am giving it as a name recycler row now we'll add one image view on the left hand side and to text you on the right vertically this image will just download image using the glide and displayed on the recycler view margin start is showing me error i'll go to gradle or update your minimum sdk version because the start start and is introduced later than the 16 version of the api now inside this linear layout i'm going to add two textures our layout is ready now we'll go back to our adapter and pass this recycler o now we'll pass parent and false and it is going to return over my view folder and will pass inflator inside the onbindview holder function we'll simply call bind and bind function inside is our myviewholder class we need a character data so we'll get it with the function get item and pass the position and it's going to pass inside the bind function now we will access our layouts image here in the text view which we added inside our recycler row xml file now we'll set the character data to our textview here we'll set the image using the glide and we need to pass the the futile which is going to check the content of the uh each row so i'm creating a separate class for this this is going to extend refutil dot item callback and it is going to hold the data type as the character data and now will simply override two functions and now we'll pass this class inside the basing data adapter that's it so our adapter is ready now now go back to our main activity and create an instance of this recycler view adapter we'll initialize it now and we'll set it to our recycler view now it's time to create our paging data source class so this class is going to take us the input of the api service that is our retro service class and it's going to extend now or pasting data paging so paging is source character data there is and we'll simply implement its two function get refresh key and load function this load function is going to get called again and again when we are loading the pages next to each other so now here we need to implement this function and version 3 library the best part of the version 3 library is like we can handle the error so since there is any exception we can pass it and we can handle that exception event here we are going to get the next page number inside the companion object i am going to create a default page that is the page number one so if key is defined then it will return the key like the page number else it will start from the base one and api service that get data from api and it will simply pass the page number now we will retrieve the pages page next page number from the api as i as we have inside the info data class like it has the page next page number so we are going to use that page number which is coming from the api so since that page number is coming as the url so we need to pass that url and just extract the page number from there if your api is returning direct page number then you don't need to do this step next page number will be passed for past value from the api it is showing me some error i think we are missing something so let me check so first thing uri.path should have the string value we found end so we need to change it in our data class we'll mark it as a string and next page number because it's still need to typecast to integer and that's it so our paging data source class is also ready now we'll use this inside our view model so let's create another view model i'm giving it as a name main activity view model we'll extend it with the view model inside the init block we'll initialize our retro service get retro instance dot create and we'll pass our retro service class interface name now we'll create a function which is actually going to call our data source paging data source class using the core routine so this flow is the part of core routine so here we'll pass the page size we have currently 34 pages so i'm going to use the same value and the max size is 200 you can have some other number as well slots factory is going to be our same class which we created as a character paging source we'll pass it here and we'll pass the retro service instance then flow dot cash in view model is cop so our view model is also ready now go back to our activity and create an instance of our view model do inside the life cycle scope this is also called from the core routine scope and here we'll use our adapter instance and call submit data and will pass it and now we'll call these two function from our oncreate function and before we run we need to add the permission as it is going to make the api call so we need to add the internet permission now let's run our application and see how it looked like so it looked absolutely fine so it's showing me the image and two values and it's loading and less learning so yeah it look good so it's keep loading all the pages as i'm scrolling it to the bottom now we can review so first of all we created a reference trans class and we implemented then we created a retro service interface and created a function and the page number we are going to pass we have a data class and which have the info and the list of character data and character data have some parameters on it then we inside our activity we initialize our view model we are initializing our view model and setting the data to our adapter then we created a paging source data source class and we are simply parsing the next page number and we are setting it to the api as you can see in the api it's returning the url so we are parsing that url and the page number we are extracting and then setting it to the next space then we came back to our view model so we are simply calling red to service and calling the view model and the data source from our view model in the adapter it's not the regular adapter so we need to extend with basic adapter and we have to implement the diff class and guys that's all for today's video thank you so much for watching
Info
Channel: LearningWorldz
Views: 5,226
Rating: 4.8333335 out of 5
Keywords:
Id: OeYlsoPQO2c
Channel Id: undefined
Length: 27min 56sec (1676 seconds)
Published: Mon Mar 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.