* The source information is from Udacity Nanodegree Program- Become an Android Developer *
S11.01- Sunshine’s New Layout:
S11.02- Sunshine Today’s List Item:
RecyclerViews allow you to inflate different layouts for each of its items, this flexibility give us the power to come up with more user friendly designs, like the one we about to do in this exercise.
To get from this old forecast layout to the new one, we will have to use a few of RecyclerView Adapter’s core functionalities:
I. Create the new layout for Today
- Add a layout called list_item_forecast_today, then use a constraint layout to implement the today list item design as shown in the image above.
II. Create booleans to personalize the experience
- Add a boolean resource called use_today_layout in res/values and set it to some default value (true or false).
- Create a resources file called bools.xml within the res/values-port directory to provide a customized boolean setting for the portrait specific layout.
- Add another boolean resource, also called use_today_layout, in res/values-port and set it to false to default the portrait layout to not show the customized Today’s design.
Now, to get the RecyclerView to know when to use which layout for its individual items; we will have to override getItemViewType. This will allow us to define different view types for different RecyclerView items based on the position.
- Override getItemViewType.
- Within getItemViewType, if our boolean resource is true and item position is 0 (first item in the list), return a custom ID for today’s viewType (call it VIEW_TYPE_TODAY).
- Otherwise, return a custom ID for future day viewType (call it VIEW_TYPE_FUTURE_DAY).
Last part would be to update onCreateViewHolder and use the returned ViewType from getItemViewType and decide on which layout to inflate:
- If the ViewType of the layout is today (VIEW_TYPE_TODAY), Inflate and use list_item_forecast_today layout.
- If the ViewType of the layout is future day (VIEW_TYPE_FUTURE_DAY), use the normal forecast_list_item layout.
- Otherwise, throw an IllegalArgumentException!
S11.03- Data Binding in Sunshine: