Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video we'll finish learning how the app works!
Project Files
-
0:00
We've got a pizza object and a topping object.
-
0:03
And when the app is created we use the toppings list
-
0:06
to generate our topping bitmaps.
-
0:09
With that out of the way let's move on to the activities.
-
0:12
We've got main activity, which was the list of pizzas.
-
0:16
And creator activity, which let's us customize the pizzas.
-
0:20
Let's start with main activity.
-
0:23
At the top of the file, we've got a pizza ID constant.
-
0:27
Other than that, we just find the recyclerview.
-
0:32
Give it a layout manager, and set it up with an adapter.
-
0:37
If you've not seen something like this before, don't worry.
-
0:41
It's just an anonymous function stored in a variable.
-
0:45
So launch creator activity is a function that takes in an integer
-
0:51
which might be null and when this function runs, it will create an intent,
-
0:57
add the pizza ID to the intent and then start creator activity.
-
1:05
So, let's jump into Main Adapter and start from the top.
-
1:09
You can use Cmd or CTRL+B to jump to a class from its usage.
-
1:14
First, we've got our LaunchCreatorActivity function as the only parameter.
-
1:19
And as we just saw it takes an integer that might be null and returns nothing.
-
1:25
From here, we have a list of pizzas which will contain the pizzas
-
1:29
to display on the screen.
-
1:31
Each row in this RecyclerView will use the pizza row layout.
-
1:39
If we take a look at that layout, under Resources and the Layout directory.
-
1:45
We can see that it contains a text view for the name of the pizza,
-
1:49
a create new pizza button and a divider line at the bottom.
-
1:55
So instead of using multiple views for the pizza rows and
-
1:58
the create new pizza button, we just used this one view and we'll hide the elements
-
2:04
we don't need depending on whether we wanna show the pizza or the button.
-
2:09
Getting back to the adapter, let's move onto the view holder.
-
2:13
Back in the code we can see that the item count is one more than the size of
-
2:18
the list, this is to account for the new pizza button at the bottom.
-
2:23
Moving on to the view holder, we show or
-
2:26
hide the views depending on which type of row it is.
-
2:30
So, if it's not the last item in the list, we'll hide the button and if it is
-
2:35
the last item in the list, we'll make the button visible and hide everything else.
-
2:41
We also set up the on click listeners to call through to our
-
2:43
launch creator activity function.
-
2:46
If it's a pizza row that was clicked, we'll pass in the ID of the pizza.
-
2:53
And if the new pizza button was clicked, we'll just pass in null.
-
2:57
So regardless of which row we click, we'll end up launching CreatorActivity.
-
3:03
Let's hop over to CreatorActivity and see where we go from here.
-
3:10
Let me give us just a little more room here as well.
-
3:15
Let me just hide it instead.
-
3:19
At the top of CreatorActivity we create a pizza ID variable and
-
3:24
lateinit it another variable named pizzaView.
-
3:28
In Cotland, when you create a variable you have to give it a value.
-
3:32
But with lateinit, we can get around that.
-
3:35
We just need to make sure it gets initialized before we use it.
-
3:39
Speaking of PizzaView, let's take a quick detour of the PizzaView class.
-
3:45
Remember, Cmd or Ctrl+B on the class name will jump us into the class.
-
3:51
PizzaView is a custom view, meaning it extends from View that draws a pizza.
-
3:57
It starts by drawing a big circle for the crust.
-
4:01
Then it draws a smaller circle for the body of the pizza.
-
4:05
And finally draws each of the toppings on top.
-
4:10
To tell it which toppings to draw, we pass in a map of toppings to Booleans.
-
4:16
If a topping is paired with the value of true, we draw it.
-
4:19
Otherwise we don't and that's about all we need to know about the PizzaView class.
-
4:25
Though, feel free to pause me and
-
4:26
look through the rest of this code if you'd like.
-
4:30
Getting back to CreatorActivity and the onCreate method,
-
4:36
we retrieve the Pizza ID from the intent and
-
4:39
initialize our PizzaView to an empty pizza, by passing in an empty map.
-
4:45
Later on when we have some data, we'll update this map to not be empty.
-
4:50
From here we add our PizzaView to the frameLayout at the top of the screen and
-
4:55
set up our recyclerView that lives at the bottom.
-
4:58
To finish out CreatorActivity,
-
5:00
we've got a couple methods to set up the menu at the top of the screen.
-
5:05
On create options menu, creates the menu and
-
5:08
on options item selected is where we add functionality to the menu items.
-
5:13
The last class we need to look at is CreatorAdaptor.
-
5:21
This class manages the recyclerView at the bottom of CreatorActivity.
-
5:26
And as a parameter, it takes in a pizza view.
-
5:29
With this pizza view at the bottom, when a switch is checked,
-
5:35
we update the toppings mapping with the state of the switch and
-
5:38
tell the pizza view to update by calling the invalidate method.
-
5:43
Awesome we've now got a good understanding of the app, and
-
5:46
are ready to implement that resistance, I know that was a lot to cover.
-
5:50
But don't worry, from here on out we'll be taking things one step at at time.
-
5:55
And if you want to spend some more time playing around and
-
5:57
getting comfortable with the app, please do.
-
5:59
I'll be here waiting for you when you get back.
You need to sign up for Treehouse in order to download course files.
Sign up