Alright so just before we move on I just wanted to clarify again this on bind
view holder. Just to finish off, that
picasso will download the image from the URL on a background thread, and it puts it into the image
view, the downloaded image, once it's downloaded.
So this method doesn't wait, so our function here, the on bind view holder, doesn't wait for the download
to finish.
And that's the image dealt with, and then this last line here on line 49, that actually puts the title into
the text view, and we're actually done.
Now the recycler view still has a reference to the view holder that it's passed in.
So when this function finishes, the recycler view can then display the holder in its list. And keep in mind
that the image may not have finished downloading at that point,
and that's why you'll see the placeholder in the list briefly, and you've seen that when I've downloaded the application
earlier in this section.
And again once the download finishes, picasso updates the image view and we get to see the thumbnail, the
actual thumbnail of the image downloaded.
Alright so at this point again, we're almost ready to test it.
The last thing we need to do though to make this work is to associate this adapter with the recycler view,
and we do that in main activity.
So we're going to open up main activity. We need to add some code in the onCreate function to do that, and we go up to the
onCreate function. Firstly though we need to, we need a field rather, to hold the recycler view
adapter.
Let's go ahead and add that at the top of the class, below the definition for our tag.
So it's going to be private val flickrRecyclerViewAdapter equals, and it's going to be FlickrRecyclerView
Adapter, parentheses ArrayList parentheses, and closing parentheses.
Right then in terms of the code in the onCreate method, we're going to add the code below the set support action bar
line on line 20, and before the call to create your uri. We're going to do recycler
underscore view dot
layoutManager equals LinearLayoutManager parentheses this.
Then the next line, recycler underscore view dot adapter equals flickrRecyclerViewAdapter.
Now the recycler view doesn't take care of handling the layouts.
That's done by a layout manager. In fact the recycler view class itself is much simpler than the list view.
It doesn't do much beyond recycling views, and everything else is left to other objects. So the data and
the view to display are provided by a recycler adapter,
the layout is performed by a layout manager and the views themselves live in a view holder.
So by delegating responsibility like this, the recycler view becomes far more flexible than a list view,
and it also performs a lot better.
Fortunately though, we don't have to write our own layout manager. We just create a new linear layout manager object,
and tell the recycler view to use that, and that's what I've done on line 24.
Now you can write your own layout manager
if you need to.
It's pretty advanced, but does mean you have full control of what's going on and how things are displayed.
Normally though it's not necessary but the option's there
if you want to. We then associate the new instance of our recycler view adapter with the recycler view, by assigning
our adapter to the recycler view's dot adapter property, or adapter property, doing that on line 25.
So at this point all that's left now to do, is put our data into the adapter.
Now the place to do that is when we receive new data, and that happens in the onDataAvailable method.
So we come down here and go back to that function that we've already created, onDataAvailable,
and we've got this code here to basically tell us the data and when it starts and ends. So I want to put some
code in there, and the code in there is going to be flickr
RecyclerViewAdapter dot loadNewData, then in parentheses
it's going to be data, data being an object which is the list of photo objects passed as a parameter to this
function.
And if you recall we created the loadNewData function to allow new data to be provided, and we're calling
it here, passing it the list of photos that we've just parsed from the json data. And what I'll do is also change
the log entry while we're here so we're not logging all that data in our log cat. So I'm just going to change that to put onData
Available called, delete the rest.
Now we can almost test the app but we've got an error in the search activity class. If we come back and
have a look there.
We deleted the floating action button from the layout, but we've still got a reference to it in onCreate.
So I'm going to delete that entire block of code.
Should fix that error and keep Android Studio happy.
So lets actually test the app now, to see what it looks like in the emulator, so I'm going to start that, and I've
got my emulator running already.
OK so you can see that that seems to be working.
It doesn't look too bad at all.
So you should be able to scroll through the photos, and you can see the placeholder object being replaced by
the actual images as the picasso library is downloading those for us,
and the recycler view is then updating itself with the view. And if we look at the log cat, we
can see there's a lot of data in there.
And what we might to do is clear this a little bit now, because we've still got a filter on there now so we can see everything,
and we can see recycler view being called, get item count being called, go right up to the top.
We can see, if we scroll down a little bit further, there's the downloading complete, onDataAvailable, onPostExecute and there's our
onCreate view holder being called, and there's our onBindViewHolder also being called for us.
And you can see that's being called continually. Basically we've seen the initial calls to onCreateViewHolder,
and then calls to onBindViewHolder as the list is scrolled.
And notice also that get item count is actually also called fairly often as well.
Bottom line here is that all the components are working together.
The downloaded data's going into the flickr rrecycler view adapter, which is feeding the recycler view fine. I'm going to stop the
video here, and in the next one we're going to extend the app, to view the full sized photo and some of
its details like the author and the tags associated with it.
So I'll see you in the next video.