RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement...

38
Copyright © 2015 CommonsWare, LLC RecyclerView, Beyond the Basics

Transcript of RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement...

Page 1: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –

Copyright © 2015 CommonsWare, LLC

RecyclerView, Beyond the

Basics

Page 2: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –

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

Page 3: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –

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

Page 4: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –

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

Page 5: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 6: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 7: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 8: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 9: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 10: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 11: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –

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”

Page 12: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –

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

Page 13: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 14: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 15: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 16: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 17: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 18: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 19: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 20: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –

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

Page 21: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 22: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 23: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 24: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 25: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 26: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 27: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 28: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 29: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 30: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 31: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 32: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 33: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 34: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –

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

Page 35: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 36: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 37: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –
Page 38: RecyclerView, Beyond the Basics - CommonsWare · Not Part of RecyclerView Itself Must implement yourself Major UI Approaches The CompoundButton – CheckBox for multiple-choice –