Adapters and the ViewHolder Pattern1:58 with Ken Alger
A brief look at how Adapters work with RecyclerViews in the MVC design pattern.
[MUSIC] 0:00 Welcome back. 0:04 We've set up our user interface, the view portion of the MVC pattern. 0:06 The model portion, 0:11 our hourglass, will need a little more work down the road, but nothing too major. 0:12 I wan to turn our attention, at least for 0:18 a little bit here, to the controller portion of the MVC design pattern. 0:20 I've mentioned adapter and view holders a couple of times now. 0:24 But what are they exactly? 0:28 And how do they all fit together? 0:30 When we use a RecyclerView, there are a few bits and pieces we need. 0:33 We have data, our array of hourly info for this project. 0:38 And we have a RecyclerView. 0:42 What we need to do to display the data in the RecyclerView? 0:44 A common design pattern in Android is to use a special component 0:48 used an adapter to, well, adapt data for the view. 0:52 We set the data for the adapter and 0:57 let the adapter do its magic to make the data look nice inside the RecyclerView. 0:59 The other pieces to the puzzle are a LayoutManager, 1:05 which manages the positioning of the items. 1:08 And a ViewHolder, which as the name would indicate, 1:11 holds the views we added to our list item layout. 1:14 Our task then are to, first, 1:18 create an Adapter to handle the data collection and bind it to the View. 1:19 Create a ViewHolder inside the Adapter. 1:25 Connect the Adapter to the Activity. 1:29 And assign a LayoutManager to manage the item position. 1:31 Something to note here. 1:36 Since we're using the Android Data Binding Library, many of the data binding tasks 1:38 that move data around the list and RecyclerView are handled for us. 1:43 There are other ways outside the Data Binding Library to accomplish these tasks. 1:47 And I've put links in the teacher's notes for further reading in case you come 1:51 across a different method in your development career. 1:55
You need to sign up for Treehouse in order to download course files.Sign up