RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement...
Transcript of RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement...
Copyright © 2015 CommonsWare, LLC
RecyclerView, Beyond the
Basics
Copyright © 2015 CommonsWare, LLC
What You Should Already Know
● Creating a Basic RecyclerView● LinearLayoutManager, custom adapter and view
holder
● Showing Dividers● Handling Clicks
● Including ripple effects on Android 5.0+
● Other Types of Layout Managers
Copyright © 2015 CommonsWare, LLC
Models and Their Collections
● No Stock RecyclerView Adapters by Collection Type● E.g., ArrayAdapter, CursorAdapter
● Any Collection Will Work● You just need to be able to do the positioning and
stuff● Example: cursors
– Database queries– Content provider queries
Copyright © 2015 CommonsWare, LLC
Models and Their Collections
● Warning: Beware Stateful Collections!● Example: cursors and positions● Problem: many view holders will be using the same
collection● Solution: do not have view holders hold the
collection, but simply bind the data in onBindViewHolder() directly
Copyright © 2015 CommonsWare, LLC
Multiple View Types
● Scenarios● List with headers● Different views for different types of models in
heterogenous list● Staggered grid with “hero element”
Copyright © 2015 CommonsWare, LLC
Multiple View Types
● Implementation● Dedicated view holder per view type
– Perhaps an interface for common operations, like binding
● getItemViewType()– Works akin to ListAdapter equivalent
● onCreateViewHolder()– Gets view type passed in as parameter, for if/switch
to instantiate the proper view holder
Copyright © 2015 CommonsWare, LLC
Choice Modes
● Not Part of RecyclerView Itself● Must implement yourself
● Major UI Approaches● The CompoundButton
– CheckBox for multiple-choice– RadioButton for single-choice
● The activated state
● Optional: Action Mode Support● Again, not part of RecyclerView
Copyright © 2015 CommonsWare, LLC
Changing the Contents
● Automatic Changes via SortedList● Implement a SortedList.Callback to find out about
each shift in the SortedList contents based on add/insert/remove operations
● Pass that information along to adapter
● Manual Changes● notifyItemInserted(), notifyItemRemoved(), etc.
● Animated Effects