Welcome back and this video, we are going to implement the functionality that allows us to click on
one of those elements here of our list, and that is something that can be done in multiple different
ways.
But I'm just going to show you one way, which is the best practice way.
So let's get started in order to do that.
What we need is, first of all.
Of course, the activity that we want to go to and the activity that we want to go to is our happy place
detail activity.
All right.
So once we want to go on that one, we, of course, want to also implement that activity.
So we need to prepare that and therefore, we have this.
Activity, heavy police detail example in which we currently only have the toolbar, so let's dial that
a little bit.
And what I want to have in there is a scroll view.
Which contains an image of you and to text you is underneath it.
All right, so in order to implement that, you can, of course, go ahead and build everything from
scratch, or you can just copy the code from the project that you can download.
So the idea behind that is that we well, we don't want to go through every single part of it.
Again, we're going to require dimensions here.
So let me paste them into my values as well.
So here dimensions.
I'm going to need quite some of them, so let me put them in here as well.
So we need a detailed screen image, height, size.
So how big should the image size be then?
Detailed screen description margin.
So how much space to want to have between that description and the other text?
And then the location has also description, textiles, location, text size.
We have a little margin between the location and the description and that's pretty much it.
All right.
So these are the dimensions.
And then I also need.
And you color and I'm just going to call this one detailed screen description, text color, so let
me go to my colors here.
And add that in their.
Which is pretty much the same as Circler border.
So what we could do, of course, is put them together into one color and then reuse it.
But in this way, we have more flexibility in terms of changing the colors.
All right.
So what is it that I have currently built right there?
So let me show you the detail activity.
So that's how it's going to look.
We have a little image here at the top.
Then we have the description and the location.
All right, and then I apparently still have an error here, so I also need, of course, to image placeholder.
So let's copy that image also into the project.
So copy it and the project that you can download and pasted in here, so it's called detailed screen
image placeholder.
So it's going to be this placeholder that you see a lot when using apps.
Right.
OK, so that is going to be the activity that we want to go to.
And of course, later on, we will populate that activity with the correct image and the correct text
and everything.
But for now, we just need to implement the functionality which allows us to move from one screen to
another by clicking onto one of the list U.
Elements or recycled view elements.
And this is a five step process, so first of all, we need to create an interface within our adapter.
So let's go to our adapter here and let's create an interface inside of that adapter.
So interface.
I'm going to call this one on Click Listener.
And in here, I will have a unclick function, which has a position of type INT.
And then the model that I want to pass, which would be of type happy place model.
All right, and of course, we can put the interface to the very bottom next to the class view holder
here.
So it's all clean and tidy.
OK, so that's step number one.
Step number two is to create a variable.
Up top here and actually inside of the glass.
Which will be our private var on click listener, which will be of type on click listener.
And it's going to be a nullable, so I'm going to sign now to it right now.
The next one is that we need to have a function which binds.
The click listener, so fun set on Click Listener, which requires an on Click listener, so on Click
Listener.
Some of those actually sort of let's type it click listener on Click Listener.
So it requires an unclick listener, we need to pass it and then in there we can go ahead and set that
so.
Here we said this on listener, it's going to be our own click listener that is passed to us.
Why do we even need to do all of that?
Well, because an adapter cannot have an unclick listener as such.
So we cannot add on click listener here, you know, like on.
That's why we need to have this little workaround, OK?
And it's not really a workaround, this really best practice, as I said, but still it's a little more
work than just adding an click listener because the actual click listener that we want to have will
be inside of our main activity because that's where we use this adapter, right?
We use this happy place adapter in our main activity because here we have our recital of you.
OK, so what we need to do is we need to set up this inside of our setup.
Happy place recycler of you.
Because in there, we create the adapter and that's exactly what we can then go ahead and use that adapter.
So we created this playsets adapter and here we can say this adapter dot set unclick listener.
And here we pass and an object and you object of happy places, adapter dot on click listener.
And now we can implement whatever we want to do with it, which means here I want to override the unclick
function, which we just created ourselves.
OK, so.
We created this unclick an our adapter.
And that was down here.
So inside of our interface, so could see we created this unclick in the interface and now we are using
it, we are overriding it.
We need to override it in our main activity.
So here we need to go ahead.
And implement whatever we want to run.
So what I want to run is I want to start a new intent.
So Voll intent is going to be intent.
I need to pass in the.
Context, which is the main activity, and then I need to tell it where I want to go, so I want to
go to happy places or place detailed activity class, the Java.
OK, so I basically just move over to this other activity with this intent, and then, of course,
I need to start this activity by using this intent here that we just created.
All right.
And then there is one last step that is left, and that is in our happy place adapter.
We need to sign that unclick listener to every single one of the different.
Roast that we have.
Because this year is an entry in our cycle view, and we can assign and unclick listeners to it by going
to our own Viant view holder because here we are binding every single view holder, which means if you
Holder is pretty much just this view that we have there.
So which is just a list element here.
And we assign up to every single one of them.
As you can see, we assign the image, we assign the title based on the description, and now we need
to do the same thing with our listener so we can just assign an only listener here so we can just go
ahead and say holder dot item, view dot, said Arncliffe, listener to the item view so few itself.
We are assigning this unclick listener in which we now can go ahead and implement our own code.
So I'm just going to check if our listener is not now.
Then I want to Rhonda on Click Listener thought on Click with the position and our model.
So what does that mean?
Well, I'm passing which position was clicked, which means which element of the respective you was
clicked.
So was it the first element, the second, the third and the fourth?
Because we need to know that in order to display the correct information on the detail of you later
on and then on passing in the model, which is basically our happy place model.
OK, so this one here, Happy Place model, where we know everything we pass in the whole thing, everything
the title, the image, the ID description and so forth, and then we can use that model in order to
populate the activity later on and display all of the relevant information, because that's basically
what we wanted in the start anyways.
Right.
But first of all, we needed to get there, so we needed to move over to the screen.
So let's go back to the adapter, because now you can see we are setting the unclick listener.
So once we click on it, we will move over to the other activity.
So you can see we call this unclick, which.
Basically is.
Going to call this unclick because we overwriting it here, we're passing in the position and the model,
where do we get it from?
Well, from the view that we clicked on and then we start the Internet or start to activity intent here,
which will then send us over to the details.
You so let's test that.
So let's go let's click on one of them, and you could see we get over to the next screen, which we,
of course, haven't implemented anything in it yet.
So just as image that we have and two empty texts which we never populated.
So that's something that we still need to do, of course.
But overall, you can see this is the procedure that you need to follow in order to implement on to
your individual list items.
And that is, of course, just one way, but it's the best way to do that.
All right.
So if you want to do it, just follow this simple sequence.
So let's go over it again.
What do we need to do to get there?
Well, we need to go to our adapter and then implement the new interface or create a new interface,
one click listener and then create a function called on Click in there, which you pass in the position,
and then some other information that you want to have, for example, the model.
Then you need to add a variable.
To that end, click listener.
Here at the very start of your class, it doesn't have to be there, but it makes sense to the start
of the class again, and it's going to be another bill at that point because we are going to.
Bindert that later on, so.
You need to create dysfunction here, said Unclick Listener, where you actually bind the unclick listener.
To the this unclick listener, so the one that your passing when calling this function, that is important.
All right, then you need to actually pass the listener to dysfunction, one calling it from the activity
in which you have to recite all of you.
OK, so in our case, it's the main activity there.
We have our recital of you, which also, of course, has the adapter.
So we create an object of the adapter, which is our happy place adapter in which we created this on
click function and the whole interface thing.
So we set the click listener.
We call this function, which we just created on Click Listener Function, then create a new happy places
on Click Listener, which is our interface and we overwrite.
It's unclick.
OK, so we overrate its function and there we do whatever we want to do, once somebody clicks on it,
it doesn't have to be an intent.
We go to another screen and this example, we want to do that, but it doesn't necessarily have to be
OK.
And then you, of course, need to bind that.
So that's the final step.
You need to bind it to the individual view holder.
So.
You go to bind view holder, so on bond, if you holder to dysfunction and then you assign it this unclick
Lessner to the holders, I tell you.
And if it exists, then you call the unclick function and here at this point, you, of course, have
the position and model, in our case, the model, but it's not necessarily just going to be the model.
You can have whatever.
Structure you want to have for unclick, but in our case, we want to put us in the position and the
model.
All right, so this is going to be necessary quite often, so pretty much every time that you want to
be able to click on list elements, so make sure that you can reproduce that.
And yeah, I said let's go to the next video.