Adding the Foundation4:43 with Ben Deitch
In this video we create another custom View for a foundation pile, and then add it to the screen!
Our deck and our waste pile are ready for action, and 0:00 it won't be long until we've got the rest of the game ready either. 0:03 But we can only take one step at a time. 0:06 So now, let's get to work on our foundation piles. 0:08 Let's start by creating a new class named FoundationPileView. 0:12 And then in the constructor, let's add in a parameter for the context. 0:22 And let's also add in a parameter for the index of this foundation pile. 0:32 Then let's make this extend ImageView and pass in our context. 0:39 And now we get a warning 0:47 about our constructor being used by tools or something. 0:50 The tools it's referring to is the Android Studio Layout tab, 0:55 which we're not using, so we can ignore this. 0:59 Next, let's add the init function and 1:03 inside let's set the imageResource to emptyPileDrawable. 1:08 Then let's add the onClick method and inside, 1:16 let's call on FoundationTap on our GamePresenter. 1:19 And pass in the index. 1:25 Up next is the update function and actually, 1:28 it's pretty similar to the one we used in our WastePileView. 1:31 So let's just copy and paste in that one to give us a head start. 1:34 Now we just need to change GameModel.wastePile 1:44 into GameModel.foundationPiles at index index. 1:49 .cards. 1:58 And we'll need to add a val up here to turn our index into a property. 2:00 Finally, we need to add the function that calls Anko view. 2:06 Again, let's just copy the one from our WastePileView. 2:10 Then let's update the function names, so foundationPileView, 2:20 and then we can copy this one and paste it over these two. 2:25 And lastly, let's add another parameter before the init function to be the index. 2:32 And then let's pass in our index as the second parameter to the constructor. 2:41 Nice, now we've just got to update MainActivity to support our new class and 2:47 we'll be good to go. 2:52 Over in MainActivity, 2:54 let's start by creating a new array to hold our four foundation piles. 2:55 val foundationPileView or 2:59 Views and let's make it an array of 3:03 foundationPileView question marks, and 3:08 let's set it equal to an arrayOfNulls of size 4. 3:14 And we don't actually need this kotlin dot part. 3:22 Next let's get rid of this last imageView and 3:27 replace it with foundationPileVews at index i. 3:31 And we're gonna set this equal to a new foundationPileView where we pass in i for 3:36 the index. 3:42 Then we just need to add .lparams, And 3:43 pass in cardWidth for the width and cardHeight for the height. 3:48 Last but not least, we just need to update in the update function. 3:52 So let's add a line at the bottom, and 3:57 then let's add foundationPileViews.forEach. 3:59 And inside, let's type it, which we can assert is not null, 4:05 .update. 4:10 Now let's run the app, and this time if you find an ace in your waste pile, 4:13 click it, and it should show up in a foundation. 4:17 And I had the ace of spades on my first try. 4:24 That is awesome. 4:27 So if I click it, it goes to a pile, great job. 4:28 We're nearing the finish line of turning our solitaire game 4:34 into a full fledged solitaire app. 4:37 In the next video, we'll finish up the UI by adding in the Tableau. 4:39
You need to sign up for Treehouse in order to download course files.Sign up