SlideShare a Scribd company logo
1 of 13
Download to read offline
1 ••
EPOXY
Building complex screens
in a RecyclerView
2 ••Modern Android Development - Epoxy - Tobias Schürg
The quick and simple way.
ListView and Adapter
list_view.adapter = ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
mutableListOf()
)
3 ••Modern Android Development - Epoxy - Tobias Schürg
Custom list adapter
class MessageAdapter(private val context: Context) : BaseAdapter() {
private val messages: MutableList<Message> = mutableListOf()
private val dateTimeInstance = DateFormat.getTimeInstance(DateFormat.MEDIUM)
fun update(newMessages: List<Message>) {
messages.clear()
messages.addAll(newMessages)
notifyDataSetChanged()
}
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val view = LayoutInflater.from(context).inflate(R.layout.chat_item, p2, false)
val message = getItem(p0)
val tvName = view.findViewById<TextView>(R.id.name)
val tvMessage = view.findViewById<TextView>(R.id.message)
val tvTime = view.findViewById<TextView>(R.id.time)
tvName.text = message.name
tvMessage.text = message.message
tvTime.text = dateTimeInstance.format(Date(message.timestamp))
return view
}
override fun getItem(position: Int): Message {
return messages[position]
}
override fun getItemId(position: Int): Long {
return messages[position].timestamp
}
override fun getCount(): Int {
return messages.size
}
}
4 ••Modern Android Development - Epoxy - Tobias Schürg
RecyclerView
• Part of the support libraries
• More advanced and flexible version of a ListView
• Does a lot of optimizations by default
• ViewHolder
• Reuse
• Animations
• Is filled by a LayoutManager (LinearLayoutManager, GridLayoutManager, …)
5 ••Modern Android Development - Epoxy - Tobias Schürg
Custom RecycleView adapter
class MyAdapter(private val myDataset: Array<String>)
: RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.MyViewHolder {
val textView = LayoutInflater.from(parent.context)
.inflate(R.layout.my_text_view, parent, false) as TextView
// set the view's size, margins, paddings and layout parameters
return MyViewHolder(textView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
// replace the contents of the view with that element
holder.textView.text = myDataset[position]
}
override fun getItemCount() = myDataset.size
}
6 ••Modern Android Development - Epoxy - Tobias Schürg
Epoxy
• Open source library by AirBnb
• Simplifies working with RecyclerView
• Avoids boilerplate
• Handles diffing
• “ready-to-run” defaults
7 ••Modern Android Development - Epoxy - Tobias Schürg
(build.gradle)
Installation
Add dependencies
implementation 'com.airbnb.android:epoxy:2.17.0‘
kapt 'com.airbnb.android:epoxy-processor:2.17.0'
Enable the Kotlin annotation processor:
apply plugin: 'kotlin-kapt'
8 ••Modern Android Development - Epoxy - Tobias Schürg
Creating models – custom views
@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class MessageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : TextView(context, attrs, defStyleAttr) {
@ModelProp
fun setValue(chatMessage: Message) {
this.text = "${chatMessage.name}: ${chatMessage.message}"
}
}
9 ••Modern Android Development - Epoxy - Tobias Schürg
Creating models – from view holders
@EpoxyModelClass(layout = R.layout.model_chat_message)
abstract class ChatMessageView : EpoxyModelWithHolder<Holder>() {
@EpoxyAttribute lateinit var name: String
@EpoxyAttribute lateinit var message: String
override fun bind(holder: Holder) {
holder.nameView.text = name
holder.messageView.text = message
}
}
class Holder : EpoxyHolder() {
lateinit var nameView: AppCompatTextView
lateinit var messageView: AppCompatTextView
override fun bindView(itemView: View) {
nameView = itemView.findViewById(R.id.name)
messageView = itemView.findViewById(R.id.message)
}
}
10 ••Modern Android Development - Epoxy - Tobias Schürg
Putting it all together
private val adapter: SimpleEpoxyAdapter = SimpleEpoxyAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
chat_recycler_view.layoutManager = LinearLayoutManager(this)
chat_recycler_view.adapter = adapter
}
override fun onResume() {
super.onResume()
adapter.addModels(createModels())
}
11 ••Modern Android Development - Epoxy - Tobias Schürg
Things to know
- All models need an ID
- model.setId(…)
- Scroll to the end of the RecyclerView with
- chat_recycler_view.scrollToPosition(adapter.itemCount - 1)
- There are @TextProp, @CallbackProp, @ModelProp (for custom views)
- @ModelProp(options = Option.DoNotHash)
- There is also an EpoxyRecyclerView for even less boilerplate
12 ••
Let’s get your
hands dirty
13 ••Modern Android Development - Epoxy - Tobias Schürg
Resources
https://github.com/airbnb/epoxy
Images: https://www.pexels.com

More Related Content

What's hot

Rntb20200805
Rntb20200805Rntb20200805
Rntb20200805t k
 
React native-firebase startup-mtup
React native-firebase startup-mtupReact native-firebase startup-mtup
React native-firebase startup-mtupt k
 
Going All-In With Go For CLI Apps
Going All-In With Go For CLI AppsGoing All-In With Go For CLI Apps
Going All-In With Go For CLI AppsTom Elliott
 
How to build and distribute CLI tool in 15 minutes with Golang
How to build and distribute CLI tool in 15 minutes with GolangHow to build and distribute CLI tool in 15 minutes with Golang
How to build and distribute CLI tool in 15 minutes with GolangKohei Kimura
 

What's hot (8)

Docker tutorial2
Docker tutorial2Docker tutorial2
Docker tutorial2
 
Rntb20200805
Rntb20200805Rntb20200805
Rntb20200805
 
React native-firebase startup-mtup
React native-firebase startup-mtupReact native-firebase startup-mtup
React native-firebase startup-mtup
 
Flamingo in Production
Flamingo in ProductionFlamingo in Production
Flamingo in Production
 
Graphql with Flamingo
Graphql with FlamingoGraphql with Flamingo
Graphql with Flamingo
 
Going All-In With Go For CLI Apps
Going All-In With Go For CLI AppsGoing All-In With Go For CLI Apps
Going All-In With Go For CLI Apps
 
How to build and distribute CLI tool in 15 minutes with Golang
How to build and distribute CLI tool in 15 minutes with GolangHow to build and distribute CLI tool in 15 minutes with Golang
How to build and distribute CLI tool in 15 minutes with Golang
 
Kitura Todolist tutorial
Kitura Todolist tutorialKitura Todolist tutorial
Kitura Todolist tutorial
 

Similar to Modern Android Development - Epoxy

C++ Windows Forms L01 - Intro
C++ Windows Forms L01 - IntroC++ Windows Forms L01 - Intro
C++ Windows Forms L01 - IntroMohammad Shaker
 
TypeScript and SharePoint Framework
TypeScript and SharePoint FrameworkTypeScript and SharePoint Framework
TypeScript and SharePoint FrameworkBob German
 
Android: a full-stack to consume a REST API
Android: a full-stack to consume a REST APIAndroid: a full-stack to consume a REST API
Android: a full-stack to consume a REST APIRomain Rochegude
 
MacRuby & RubyMotion - Madridrb May 2012
MacRuby & RubyMotion - Madridrb May 2012MacRuby & RubyMotion - Madridrb May 2012
MacRuby & RubyMotion - Madridrb May 2012Mark Villacampa
 
Android application architecture
Android application architectureAndroid application architecture
Android application architectureRomain Rochegude
 
Women Who Code - RSpec JSON API Workshop
Women Who Code - RSpec JSON API WorkshopWomen Who Code - RSpec JSON API Workshop
Women Who Code - RSpec JSON API WorkshopEddie Lau
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on AndroidSven Haiges
 
What is the ServiceStack?
What is the ServiceStack?What is the ServiceStack?
What is the ServiceStack?Demis Bellot
 
What is the ServiceStack?
What is the ServiceStack?What is the ServiceStack?
What is the ServiceStack?Demis Bellot
 
Plone FSR
Plone FSRPlone FSR
Plone FSRfulv
 
What istheservicestack
What istheservicestackWhat istheservicestack
What istheservicestackDemis Bellot
 
What is the ServiceStack?
What is the ServiceStack?What is the ServiceStack?
What is the ServiceStack?Demis Bellot
 
Laravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swaggerLaravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swaggerLaravel Poland MeetUp
 
OCCIware Year 1 Milestone: Docker Studio, Studio Factory, pluggable XaaS runt...
OCCIware Year 1 Milestone: Docker Studio, Studio Factory, pluggable XaaS runt...OCCIware Year 1 Milestone: Docker Studio, Studio Factory, pluggable XaaS runt...
OCCIware Year 1 Milestone: Docker Studio, Studio Factory, pluggable XaaS runt...OCCIware
 
OCCIware Cloud Expo London 2016 - Docker Studio, Studio Factory, erocci bus &...
OCCIware Cloud Expo London 2016 - Docker Studio, Studio Factory, erocci bus &...OCCIware Cloud Expo London 2016 - Docker Studio, Studio Factory, erocci bus &...
OCCIware Cloud Expo London 2016 - Docker Studio, Studio Factory, erocci bus &...Marc Dutoo
 
ASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web AppsASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web AppsShahed Chowdhuri
 
CouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 HourCouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 HourPeter Friese
 
What's new and noteworthy in Java EE 8?
What's new and noteworthy in Java EE 8?What's new and noteworthy in Java EE 8?
What's new and noteworthy in Java EE 8?gedoplan
 

Similar to Modern Android Development - Epoxy (20)

Play!ng with scala
Play!ng with scalaPlay!ng with scala
Play!ng with scala
 
C++ Windows Forms L01 - Intro
C++ Windows Forms L01 - IntroC++ Windows Forms L01 - Intro
C++ Windows Forms L01 - Intro
 
TypeScript and SharePoint Framework
TypeScript and SharePoint FrameworkTypeScript and SharePoint Framework
TypeScript and SharePoint Framework
 
Android: a full-stack to consume a REST API
Android: a full-stack to consume a REST APIAndroid: a full-stack to consume a REST API
Android: a full-stack to consume a REST API
 
MacRuby & RubyMotion - Madridrb May 2012
MacRuby & RubyMotion - Madridrb May 2012MacRuby & RubyMotion - Madridrb May 2012
MacRuby & RubyMotion - Madridrb May 2012
 
Android application architecture
Android application architectureAndroid application architecture
Android application architecture
 
Women Who Code - RSpec JSON API Workshop
Women Who Code - RSpec JSON API WorkshopWomen Who Code - RSpec JSON API Workshop
Women Who Code - RSpec JSON API Workshop
 
CouchDB on Android
CouchDB on AndroidCouchDB on Android
CouchDB on Android
 
What is the ServiceStack?
What is the ServiceStack?What is the ServiceStack?
What is the ServiceStack?
 
5.node js
5.node js5.node js
5.node js
 
What is the ServiceStack?
What is the ServiceStack?What is the ServiceStack?
What is the ServiceStack?
 
Plone FSR
Plone FSRPlone FSR
Plone FSR
 
What istheservicestack
What istheservicestackWhat istheservicestack
What istheservicestack
 
What is the ServiceStack?
What is the ServiceStack?What is the ServiceStack?
What is the ServiceStack?
 
Laravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swaggerLaravel dokumentacja Restful API - swagger
Laravel dokumentacja Restful API - swagger
 
OCCIware Year 1 Milestone: Docker Studio, Studio Factory, pluggable XaaS runt...
OCCIware Year 1 Milestone: Docker Studio, Studio Factory, pluggable XaaS runt...OCCIware Year 1 Milestone: Docker Studio, Studio Factory, pluggable XaaS runt...
OCCIware Year 1 Milestone: Docker Studio, Studio Factory, pluggable XaaS runt...
 
OCCIware Cloud Expo London 2016 - Docker Studio, Studio Factory, erocci bus &...
OCCIware Cloud Expo London 2016 - Docker Studio, Studio Factory, erocci bus &...OCCIware Cloud Expo London 2016 - Docker Studio, Studio Factory, erocci bus &...
OCCIware Cloud Expo London 2016 - Docker Studio, Studio Factory, erocci bus &...
 
ASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web AppsASP.NET Core 2.1: The Future of Web Apps
ASP.NET Core 2.1: The Future of Web Apps
 
CouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 HourCouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 Hour
 
What's new and noteworthy in Java EE 8?
What's new and noteworthy in Java EE 8?What's new and noteworthy in Java EE 8?
What's new and noteworthy in Java EE 8?
 

Recently uploaded

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456KiaraTiradoMicha
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 

Recently uploaded (20)

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 

Modern Android Development - Epoxy

  • 1. 1 •• EPOXY Building complex screens in a RecyclerView
  • 2. 2 ••Modern Android Development - Epoxy - Tobias Schürg The quick and simple way. ListView and Adapter list_view.adapter = ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, mutableListOf() )
  • 3. 3 ••Modern Android Development - Epoxy - Tobias Schürg Custom list adapter class MessageAdapter(private val context: Context) : BaseAdapter() { private val messages: MutableList<Message> = mutableListOf() private val dateTimeInstance = DateFormat.getTimeInstance(DateFormat.MEDIUM) fun update(newMessages: List<Message>) { messages.clear() messages.addAll(newMessages) notifyDataSetChanged() } override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View { val view = LayoutInflater.from(context).inflate(R.layout.chat_item, p2, false) val message = getItem(p0) val tvName = view.findViewById<TextView>(R.id.name) val tvMessage = view.findViewById<TextView>(R.id.message) val tvTime = view.findViewById<TextView>(R.id.time) tvName.text = message.name tvMessage.text = message.message tvTime.text = dateTimeInstance.format(Date(message.timestamp)) return view } override fun getItem(position: Int): Message { return messages[position] } override fun getItemId(position: Int): Long { return messages[position].timestamp } override fun getCount(): Int { return messages.size } }
  • 4. 4 ••Modern Android Development - Epoxy - Tobias Schürg RecyclerView • Part of the support libraries • More advanced and flexible version of a ListView • Does a lot of optimizations by default • ViewHolder • Reuse • Animations • Is filled by a LayoutManager (LinearLayoutManager, GridLayoutManager, …)
  • 5. 5 ••Modern Android Development - Epoxy - Tobias Schürg Custom RecycleView adapter class MyAdapter(private val myDataset: Array<String>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() { class MyViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.MyViewHolder { val textView = LayoutInflater.from(parent.context) .inflate(R.layout.my_text_view, parent, false) as TextView // set the view's size, margins, paddings and layout parameters return MyViewHolder(textView) } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { // replace the contents of the view with that element holder.textView.text = myDataset[position] } override fun getItemCount() = myDataset.size }
  • 6. 6 ••Modern Android Development - Epoxy - Tobias Schürg Epoxy • Open source library by AirBnb • Simplifies working with RecyclerView • Avoids boilerplate • Handles diffing • “ready-to-run” defaults
  • 7. 7 ••Modern Android Development - Epoxy - Tobias Schürg (build.gradle) Installation Add dependencies implementation 'com.airbnb.android:epoxy:2.17.0‘ kapt 'com.airbnb.android:epoxy-processor:2.17.0' Enable the Kotlin annotation processor: apply plugin: 'kotlin-kapt'
  • 8. 8 ••Modern Android Development - Epoxy - Tobias Schürg Creating models – custom views @ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT) class MessageView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : TextView(context, attrs, defStyleAttr) { @ModelProp fun setValue(chatMessage: Message) { this.text = "${chatMessage.name}: ${chatMessage.message}" } }
  • 9. 9 ••Modern Android Development - Epoxy - Tobias Schürg Creating models – from view holders @EpoxyModelClass(layout = R.layout.model_chat_message) abstract class ChatMessageView : EpoxyModelWithHolder<Holder>() { @EpoxyAttribute lateinit var name: String @EpoxyAttribute lateinit var message: String override fun bind(holder: Holder) { holder.nameView.text = name holder.messageView.text = message } } class Holder : EpoxyHolder() { lateinit var nameView: AppCompatTextView lateinit var messageView: AppCompatTextView override fun bindView(itemView: View) { nameView = itemView.findViewById(R.id.name) messageView = itemView.findViewById(R.id.message) } }
  • 10. 10 ••Modern Android Development - Epoxy - Tobias Schürg Putting it all together private val adapter: SimpleEpoxyAdapter = SimpleEpoxyAdapter() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) chat_recycler_view.layoutManager = LinearLayoutManager(this) chat_recycler_view.adapter = adapter } override fun onResume() { super.onResume() adapter.addModels(createModels()) }
  • 11. 11 ••Modern Android Development - Epoxy - Tobias Schürg Things to know - All models need an ID - model.setId(…) - Scroll to the end of the RecyclerView with - chat_recycler_view.scrollToPosition(adapter.itemCount - 1) - There are @TextProp, @CallbackProp, @ModelProp (for custom views) - @ModelProp(options = Option.DoNotHash) - There is also an EpoxyRecyclerView for even less boilerplate
  • 12. 12 •• Let’s get your hands dirty
  • 13. 13 ••Modern Android Development - Epoxy - Tobias Schürg Resources https://github.com/airbnb/epoxy Images: https://www.pexels.com