All right, so we have this recycle of you here, and I want to use this with you binding and up to
Android 10.
Caitlin extensions would have been the solution here, so you wouldn't even use view binding before
that.
You would probably just use find view by ID and so forth.
But nowadays you can, of course, still use find view by ID in order to connect a view to their binding
classes.
But the latest approach and the one that Google recommends is view binding and view binding in normal
use is quite simple, but when it comes to view binding with recyclable use, then it's a different
story.
And in this video, we are going to look at how we can enable view binding and use view binding with
our recyclable use.
So let's get started.
All right, so I'm going to start with an empty activity, and by the way, I'm using Android 12 in
the background, even though anything related in that shouldn't be a problem either way.
So this one will be view binding RV.
All right.
Everything else stays the same.
You can, of course, change the Android version if you feel like it, but Android five should be fine.
All right, so once in your project, of course, you have your main activity and all of the good stuff.
But what's going to be important is that you go over to your built Gradle and you add a dependency.
And it's going to be this bill like cradle.
And we need to make sure that we have the build features in here.
So inside of here, you had build features like so and then in curly brackets, you add that you want
to activate view binding.
So you just said view binding to truth.
All right.
And once you're done with that, you can sync.
It is really important to think otherwise, your whining will not be activated and all of the following
steps won't work.
So in order to create a recycler view and display a list, we will need the following setup so we need
a recycled view, an item layout, a view holder and an adapter.
And if you don't know what all of these things are, you should definitely check out my video and recycle
views because even though it's one and a half years old, it's still exactly the same.
So first of all, I'm going to go to my resources and I'm going to go over this really quickly.
Right?
So Activity Maine example, and we're going to use ExxonMobil here, and I'm not jetpack compose, which
we're going to create a video on later.
But here, instead of using all of the stuff that you see here, I'm going to use that stuff here.
So basically what we have is we had a recycled view in here instead of using a text you.
So generally, you need to use this recycled view keyword inside of Android to recycle your widget,
and then it will create this recycle of view.
And unfortunately, there's too much happening here.
So this is basically going to be the appearance you have item one, two, three and so forth.
Now you can, of course, modify how each item has to look like.
And therefore, you create a new item layout.
OK, so let's really quickly do that.
Therefore, I can just write in here and create a new layout resource file.
Let me call that recycler view on the score item and then the root element here could stay with you
and we can also leave layout.
That's going to be fine.
So let's go back to the cold because it's a lot easier to edit, at least in my opinion.
And then what we want to achieve is a simple to do list, right?
So like we have it in this app where we have.
Well, take a walk, then eat breakfast and so forth.
So of course, you could uncheck them or check them with an additional item at the right hand corner.
But this would you should really focus on just how to re-use view binding in a recycling view.
OK, so here I'm just going to, first of all, make sure that I'm using the constraint layout and my
constraint layout is going to look like this these namespaces.
For now, they're not used, but it's fine.
So I'm going to add a little bit of padding.
Then I want to have a text view inside of this constrained layout, as well as another text view.
And then finally, I want to have a view in here.
So you see here it has the title, then it has the time.
And then finally, we want to have a little view which will create a background.
So basically, if we look at it, this is what it's going to be.
So we have for every single item this appearance.
So title and then let me scroll even further title time and then this little line that we added.
So this view here is the line which has this grayish background.
You can see it here.
You can, of course, change it by selecting a custom color.
There's a recall in the newest Android Studio versions.
You can just click on the color and change it.
I love that.
All right.
So then we now have the individual item that we want to use for our activity main.
So every single item here should look like this type of view item that we have just set up.
For now, if you want to display this without having to test it straight away, you can go to your activity
main exam.
And you can make a change to your recycle view here.
And what you can do is you can just add a tools statement.
So here tools and you can define the list item property.
So here I'm just going to say in my layout folder, I have this file called recycler view item.
And now it will use this XML file, which is created as the tools display, so it will just displayed
here.
Basically, it's not much more than that.
It's not going to impact our code itself.
That's something we will have to change.
But for our development process, this will be a lot nicer to look at, I believe.
OK, so now you can see how each item is going to look like, and that happens by using this tools option
here.
We just say, OK, I want to list items to be my recycle your items.
And now comes the more complex part, and that is going to be to create an adapter.
Well, the adapter needs a model, so let's create a data class and name it task.
So inside of my examples here, I'm going to create a new Scotland class, I'm going to call this one
task like so and this will not be a general class, but it will be a data class.
OK, so data class, which will be of type task.
And here I use brackets, not the curly ones, but the run at once.
And I want to have a title which will be of type string and I want to have time.
So here timestamps like so and they are not going to be of type style timestamp, but they are going
to be of type string as well.
All right.
So then of course, we can get rid of this import.
OK, so this is going to be the data class that I want to use.
Now, of course, we will need a task list as well, which I'm just going to create an object of.
I'm going to keep it super simple.
So here, let's create a new Kotlin class file and then go to select Object here.
I'm going to call this one task list, and this is really just our data source, so to speak.
OK, you need a data source, and you could get the data from your room database if you want to use
that or from your API or whatever.
But we're going to keep it simple.
I'll just create a list of a bunch of items.
So here a task list and this list of items is going to use the Tasker.
And by the way, I'm seeing here that I misspelled timestamp.
So it should be with a capital s so that it fits my object structure that I'm using.
So here I'm creating a list of tasks and each task as a title, as well as a timestamp.
So it's just a text in form of a string here, which is going to be a number and eat dinner at seven
a.m. So if you were partying really hard and you didn't have dinner before, then maybe having breakfast
is like having dinner.
So we can, of course, change that 2:00 PM as well.
All right.
So now let's go over to our main adopted that we need to set up, which of course now will use the hard
task, as well as the task list as the data source.
OK, so right click here and create a new class, and I'm going to call this one main adaptor.
OK, so now in our main adapter, let's go ahead and make sure that this is going to be a type recycler
view because that's what our view is like, right?
Recycler view.
And then I need to make sure that this is going to be the adapter and I'm going to use main adapter,
the main Vue holder like.
So again, not the main Vue holder, I will need to create an inner class in order to be able to use
it here.
So let's create an inner class main view holder here, which will have an item binding called recycler
view item binding.
And then it's going to be inheriting from recycler view dot view holder like so.
And here we need your the item binding, and we can use the root value from it.
So the constraint layout that we are using.
OK, so let me put that into a second line so that it will be a little more readable.
So this is going to be in a class and inside of this inner class.
We need to do one thing and that is to bind our item.
So here we bind the item with the task.
So we'd say.
The data for the item should get more should be gotten from the task.
So now that by itself is not going to be enough, we need to state what information of the task should
be used, for which element of our recycled view item.
OK, so here if you look at it, this recycled view item has two properties that are relevant to one
of them is the texture that says title TV and then the other one is time TV, because this third one
is just a view that doesn't have to be used at all.
It's just this gray line, right?
So how can we connect our main adapter to the.
Recycle of used title.
Well, we can simply do that by just saying here that we want to use the item binding that is passed
to the main view holder.
This one here and we want to use its time while here it's the title TBE text.
So the text property of the title text view to be connected to the task title.
So not the time stamp, but the title.
And all of the same goes for the other item that we want to access on here.
The other item diet or text view was this time stamp text you here and this is not going to be title,
but time TV.
So here the text should be task dot time stamp.
Like so, so now we're done with the view binding, but that by itself is, of course not enough because
we also need to implement.
So the members that are main adapt their needs.
So let's implement them.
Select them all three with the shift key and click OK.
And now you see we get the uncreated view holder on Biden for you, holder and on item count.
Now let's implement those.
The easiest one is always the item count, in my opinion, because it's just a one liner, basically.
And here we're just going to return the length of our or the size of our task list.
So your task list, that size now, where's the task this coming from?
Well, let's check it out.
The thing is, we need to pass it at the main adapter level.
So here are the very top.
We are creating our main adapter, which inherits from the recycle of you, but we didn't pass any details
to it.
So now let's pass the task list to it, which is basically a list of tasks, right?
So it's going to be this task list here that we want to use in our main adapter.
And now we're passing it over here and we are finding out what is the size and then we're displaying
the amount of items that we want to display there.
Now what should happen once we create our view holder?
So once our recycler view is created, well, here we need to return a view holder because it says it
returns and main view holder.
So let's do that.
Let's just return to my view holder like Zoom and let's pass the items.
And in order to pass the items, we need to reinflate the binding layout.
So we will return the main view holder inflating the binding layout.
This is how the item layout is inflated when using view binding.
So this is the part that is a little different to and while too old fashioned to produce.
So here we're not binding, but we're inflating it here.
So now that's inflated.
And if you want to inflate it, you see you need to have an layout inflator and you can add additional
info about the parent and the attached parent.
So here I'm just going to say I want to use the layout and fled or from the context of the parent.
So here from parent context.
So this by itself, as you see, doesn't cause any issues.
So the code seems to be all right.
But if you want to be sure, you can also add the parent as well as the flag falls for the attach to
parent.
So now what should happen once we bind the view holder so on bind view holder?
Well, here I just want to make sure that the task is going to be displaying the task lists position
item.
So he had task list and the item at the position of the individual item.
Because if we look at our recycling bin here, you can see we have the individual items, but every
single item needs to know which data it should get and it's going to get the data based on its position.
So take a walk here gets the position zero, which is this take a walk.
Then the next item in the list will be eat breakfast, so it will be this item and so forth, right?
So this is position zero in the list.
This is position one in the list, position two and so forth.
So that's basically how our main adapter gets the data.
So now the task itself is not enough because, well, we're not using it, right?
So now let's actually use it by using the holder and bind the item.
So now we are binding our task to the holder, to the main view holder.
And what this will allow us to do is basically to, well, bind data to recycler view using view binding.
So let's recap what we did here.
So first, we created a variable for the adaptors list in the main adapter constructor.
Then we'll return the main view holder in the on create view holder and inflate a layout by passing
in recycler view item binding inflate.
So this whole part here where we are passing this resectable, you inflate stuff to the constructor,
so we pass it to the main view holder constructor.
Then we bind each of the items in the unbind view holder and we get the item count.
There's always something we need to return so that the recycler view knows how many items it needs to
display.
So now we have a list of items of task items, so to speak, and let's actually bind all of this stuff
in our main activity because currently it's empty, right?
So this unfortunately, if you run this application, it's not going to be enough.
It's not going to do anything because the main activity doesn't know about the recycle of you and the
main adapter that we have set up.
And all of this stuff will just be an empty screen, so to speak, an empty white space because, well,
it's going to use our activity main, but it doesn't understand what the recycler view is that it should
display.
So let's fix that.
You see here, OK, find a view binding RV doesn't find it.
OK, so the first thing we need to do here is we need to set up our binding.
So let's set up activity main binding, which can be a Nullarbor.
So let's add the question mark.
And then I'm going to say no, because I want this to be null at the beginning, and then I'm going
to essentially edit inside of my own create.
So here let's use binding equals activity, mean binding bind.
And here we don't bind it, but we inflated.
So you see, the IEEE always wants me to bind it, but I'm actually wanting to inflate it, and I'm
going to use the layout inflator here in order to inflate it.
So if you want to know more about any of the items you're wondering about, just hold the control key
and click on the item and you'll find out more about it.
So it's just publicly out inflator, which gets the layout inflator and returns to get window with a
layout and fader, so you can even go further to find out what this method is, and it's an abstract
method.
All right, so now.
That we have that bound, let's set the content view, which we did here using the R layout, but I'm
going to use it differently.
So let me get rid of this line.
I'm going to now use my binding that I just set up.
So content view what I'm going to pass my binding, which is the Nullarbor route.
OK, so because it's a Nullarbor, well, we don't need it semicolons here, but it's a Nullarbor.
So I'm going to add the optional question mark here.
And now we need to set up the adapter because we need to bind the adapter.
So here, let's create an adapter object of our main adapter because otherwise?
Well, why would he have prepared it?
So main adapter with the task list of items?
So we're going to use the task list object here, which contains this task list, which contains this
task, a list of list items.
OK, so that's the object name and that is the name of the list of task objects.
So let's use that in our main activity here.
We assign it as the data for our main adapter because if you look at the main adapter, it requires
a task list because that's what we set up in the constructor here.
So we say we want to have a list of tasks and beautifully enough.
This is a list of tasks now that we have set up that let's go ahead and combine everything.
So I'm going to use my binding and I'm going to get its task RV.
So what is this task RV?
Well, it's this recycler view that we had set up in our activity next summer.
OK, so here.
This is the ID task RV, so we are accessing it by using the binding keyword, which is, well, using
it to what you mean by adding, we're inflating the layout.
Now we're using the recycle view in order to assign an adapter to it.
And this task, RV can also be null.
So let's make that clear here as well, and let's just use our adapter.
All right, so now we have assigned the adapter, and now the only thing that's important and left to
do is to make sure that we are destroying so often destroy our binding object.
Once the on destroying method is called, so we just set it to know that's just common practice, and
it's very important to do that because otherwise, well, it's just not clean.
It could break your application.
So we are saying, OK, once on this process called let's deactivate all of the binding, so nullified
this binding object.
So how is this different to what we did before?
Well, here in this content view, that's really the main point we used as binding here instead of directly
passing the layout.
So what we had before was the set content view, which would just go ahead and say our out layout.
So it's sort of the layout folder.
I want to use the file called activity main, right?
So this is the default that we have, but we don't want that, even though, of course, we want to
use this main activity, but we want to use it using data binding, which is why we don't use this line
here, but we use this one here.
And for this to work, we need to make sure that our binding is set up and inflated, and therefore
we need to use this activity main binding and inflated with our layout and inflator, and we get the
route, which is this constraint layout.
OK.
So we just say, OK, use this constraint layout and use it in our content view.
So set it up with the entire activity.
Main page but using its route and the route, of course, contains all of the details.
But then we're setting the main adapter, which is this one here and now we just need to make sure that
we assign the right layout manager to our adapter so we could either do it in Gotland code directly
or we can go over to our activity.
Main example.
And here we can assign a layout manager, and the one that I want to use is going to be a linear layout
manager, which is just going to display the items on top of each other.
OK, so now let's run the application and see if this is going to display the details.
And you see here, there they are.
So now let's add another timestamp here.
Go to bed and then him should add a comma.
And this one will be midnight snack midnight snack, which is not a good idea, by the way.
I'm not sure how this works.
Probably zero eight.
Like, I don't know how the American system works here, but now let's run it again.
Let's not ask again, and we can see here we have our midnight snack.
Zero a.m..
All right.
So that's basically how you can bind using a recycler of you with an adapter and combining everything.
You can use your.
Fast, are we here?
And use the lay out manager like sole and assigned a linear layout.
So here El linear layout manager, like so and you need to pass a couple of parameters to it.
So first of all, the context and then the direction you want to use.
So I will just use vertical in here and I'm going to use falls as the reverse layout, OK, not reversing
it.
So then in that case, you could get rid of this line here and not assign the layout manager inside
of the XML file, but instead inside of your codling code.
So let's rerun the application and see if our code is still going to work and we can see it still works
flawlessly.
So this would have been the alternative way of doing it.
So this is in code and this is in the UI item.
All right.
So I hope you enjoyed the.