SlideShare a Scribd company logo
1 of 58
Download to read offline
The Tale of a Smooth RecyclerView
ADAM ŠIMEK
Knowing how to code lists is
important
Kulman’s test
But why?
¯_(ツ)_/¯
16
16ms
“Well, that’s: user input, networking, data
processing, database/disk I/O, view inflation,
layout, drawing,… Thwre also other
applications, cpus are slow, memories are too,
everything takes forever. And then you are
supposed to save battery.”
16ms?
Let’s get started
ListView
ListView
RecyclerView
RecyclerView Architecture
ViewHolder
ViewHolder
Best practice from ListView

Lightweight view wrapper for list items

Recycler holds a limited amount of them and reuses them

One ViewHolder is used for more items (not at a time)

It has it’s own lifecyle
ViewHolder Lifecycle
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {}
@Override
public void onViewAttachedToWindow(ViewHolder holder) {}
@Override
public void onViewDetachedFromWindow(ViewHolder holder) {}
@Override
public void onViewRecycled(ViewHolder holder) {}
@Override
public boolean onFailedToRecycleView(ViewHolder holder) {}
ViewHolder Lifecycle
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {}
@Override
public void onViewAttachedToWindow(ViewHolder holder) {}
@Override
public void onViewDetachedFromWindow(ViewHolder holder) {}
@Override
public void onViewRecycled(ViewHolder holder) {}
@Override
public boolean onFailedToRecycleView(ViewHolder holder) {}
“The fastest code is the code that never runs.”
Sokrates
What not to do?
Disk I/O - SQLite, SharedPreferences, Content Providers

for (Comment comment : item.getComments()) {}
Parsing of text, JSONu,…, complicated formatting,..

Html.fromHtml()
Complicated layouts 🙂

“I can go home, it’s runs smooth on my phone.”

Critical code can be hidden (deep) in the abstraction
What to do?
Everything expensive has to run in background

…,or it has to be preprocessed

You don’t have to show it right away → use tmp placeholders

void onScrollStateChanged(…, int newState)
mHandler.postDelayed(deferredBind, DELAY }
☝ it’s crucial to keep this balanced

Test on various devices (low-end, tablets)

…and with various data

StrictMode
Recycling saves our planet.
Turns out it can save your app too.
Guess what’s wrong?
//ViewHolder code called in onBindViewHolder()
@Override
public void bindData(Object data, final int position) {
mImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mClickHandler.onItemClicked(v, position, photo);
}
});
}
Guess what’s wrong?
//ViewHolder code called in onBindViewHolder()
@Override
public void bindData(Object data, final int position) {
mImageView.setOnClickListener(mClickListener); // use member var instead
}
Guess what’s wrong?
//ViewHolder code called in onBindViewHolder()
@Override
public void bindData(Object data, final int position, ItemClickListener listener) {
mImageView.setOnClickListener(listener); // even better..
}
Guess what’s wrong?
//ViewHolder code called in onBindViewHolder()
@Override
public void bindData(Object data, final int position) {
mImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mClickHandler.onItemClicked(v, position, photo);
}
});
}
Guess what’s wrong?
//ViewHolder code called in onBindViewHolder()
@Override
public void bindData(Object data, final int position) {
mImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mClickHandler.onItemClicked(v, getAdapterPosition(), photo);
}
});
}
Measure twice, cut once.
AsyncTask
AsyncTask
Runs tasks in serial → super slow

Hm…

executeOnExecutor(THREAD_POOL_EXECUTOR);
Big operational overhead

Tricky and unreliable task cancelling
💥
java.util.concurrent.RejectedExecutionException: Task
android.os.AsyncTask$3@36e83ee rejected from
java.util.concurrent.ThreadPoolExecutor@a54e08f[Running, pool size = 13,
active threads = 13, queued tasks = 128, completed tasks = 1055]
DIY
Custom “taskmanager”

…
mDecodeThreadPool = new ThreadPoolExecutor(
NUMBER_OF_CORES, // Initial pool size
NUMBER_OF_CORES, // Max pool size
KEEP_ALIVE_TIME,
KEEP_ALIVE_TIME_UNIT,
mDecodeWorkQueue); // 128 size in AsyncTask
…
mDecodeThreadPool.execute(DownloadTask); // execute task
…
Sneaky things
Sneaky things
View.requestLayout()
———————
RecyclerView.setHasFixedSize(
boolean hasFixedSize
)
———————
TextView.setAllCaps(boolean allCaps)
———————
notifyDatasetChanged()
View.requestLayout()
View.requestLayout() + hasFixedSize
void triggerUpdateProcessor() {
if (POST_UPDATES_ON_ANIMATION && mHasFixedSize && mIsAttached) {
ViewCompat.postOnAnimation(RecyclerView.this, mUpdateChildViewsRunnable);
} else {
mAdapterUpdateDuringMeasure = true;
requestLayout();
}
}
Sneaky things
View.requestLayout()
———————
RecyclerView.setHasFixedSize(
boolean hasFixedSize
)
———————
TextView.setAllCaps(boolean allCaps)
———————
notifyDatasetChanged()
TextView.setAllCaps()
public void setAllCaps(boolean allCaps) {
if (allCaps) {
setTransformationMethod(new AllCapsTransformationMethod(getContext()));
} else {
setTransformationMethod(null);
}
}
Sneaky things
View.requestLayout()
———————
RecyclerView.setHasFixedSize(
boolean hasFixedSize
)
———————
TextView.setAllCaps(boolean allCaps)
———————
notifyDatasetChanged()
“Today I will do what others won't, so
tomorrow I can accomplish what
others can’t"
Prefetch
Systrace
Systrace - missed frame
Prefetch OFF
Prefetch ON
Prefetch
Create and bind “up front”

Estimates how long it will run to make sure it won’t cause delays

Lollipop+ (RenderThread), support lib 25.1

setItemPrefetchEnabled(true)
Supported by built-in LayoutManageres (API for custom ones)

Watch out for onBindViewHolder - no animations,…

setInitialPrefetchItemCount(N)
You can see the difference with your naked eye 👀
Thank you!
@simekadam
strava.com/athletes/simekadam
@simekadam
strava.com/athletes/simekadam

More Related Content

What's hot

Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo KumperaAdvanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Xamarin
 
Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)
Gilbok Lee
 

What's hot (15)

Indexed db
Indexed dbIndexed db
Indexed db
 
Correcting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await MistakesCorrecting Common .NET Async/Await Mistakes
Correcting Common .NET Async/Await Mistakes
 
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoTJSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
JSDC 2017 - 使用google cloud 從雲到端,動手刻個IoT
 
Discussion of NGRX-Entity
Discussion of NGRX-EntityDiscussion of NGRX-Entity
Discussion of NGRX-Entity
 
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo KumperaAdvanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
Advanced Memory Management on iOS and Android - Mark Probst and Rodrigo Kumpera
 
Users' Data Security in iOS Applications
Users' Data Security in iOS ApplicationsUsers' Data Security in iOS Applications
Users' Data Security in iOS Applications
 
TDD With Typescript - Noam Katzir
TDD With Typescript - Noam KatzirTDD With Typescript - Noam Katzir
TDD With Typescript - Noam Katzir
 
Workers
WorkersWorkers
Workers
 
Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)Data Binding Intro (Windows 8)
Data Binding Intro (Windows 8)
 
Fetch data from form
Fetch data from formFetch data from form
Fetch data from form
 
Modern Android app library stack
Modern Android app library stackModern Android app library stack
Modern Android app library stack
 
Getting Reactive with Cycle.js and xstream
Getting Reactive with Cycle.js and xstreamGetting Reactive with Cycle.js and xstream
Getting Reactive with Cycle.js and xstream
 
Creating Applications with WebGL and Three.js
Creating Applications with WebGL and Three.jsCreating Applications with WebGL and Three.js
Creating Applications with WebGL and Three.js
 
Selectors and normalizing state shape
Selectors and normalizing state shapeSelectors and normalizing state shape
Selectors and normalizing state shape
 
Thunderstruck
ThunderstruckThunderstruck
Thunderstruck
 

Similar to The Tale of a Smooth RecyclerView

JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
Robert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
Robert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
Robert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
Robert Nyman
 
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos AiresJavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
Robert Nyman
 
YUI for control freaks - a presentation at The Ajax Experience
YUI for control freaks - a presentation at The Ajax ExperienceYUI for control freaks - a presentation at The Ajax Experience
YUI for control freaks - a presentation at The Ajax Experience
Christian Heilmann
 
10 ways to make your code rock
10 ways to make your code rock10 ways to make your code rock
10 ways to make your code rock
martincronje
 

Similar to The Tale of a Smooth RecyclerView (20)

Android Architecture Components
Android Architecture ComponentsAndroid Architecture Components
Android Architecture Components
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Android UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesAndroid UI Tips, Tricks and Techniques
Android UI Tips, Tricks and Techniques
 
Android UI Development: Tips, Tricks, and Techniques
Android UI Development: Tips, Tricks, and TechniquesAndroid UI Development: Tips, Tricks, and Techniques
Android UI Development: Tips, Tricks, and Techniques
 
Don't Make Android Bad... Again
Don't Make Android Bad... AgainDon't Make Android Bad... Again
Don't Make Android Bad... Again
 
Architecture components - IT Talk
Architecture components - IT TalkArchitecture components - IT Talk
Architecture components - IT Talk
 
Architecture Components
Architecture Components Architecture Components
Architecture Components
 
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, ChileJavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
JavaScript APIs - The Web is the Platform - MDN Hack Day, Santiago, Chile
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao PauloJavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
 
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos AiresJavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
JavaScript APIs - The Web is the Platform - MDN Hack Day - Buenos Aires
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
Writing Maintainable JavaScript
Writing Maintainable JavaScriptWriting Maintainable JavaScript
Writing Maintainable JavaScript
 
droidQuery: The Android port of jQuery
droidQuery: The Android port of jQuerydroidQuery: The Android port of jQuery
droidQuery: The Android port of jQuery
 
SOLID
SOLIDSOLID
SOLID
 
303 TANSTAAFL: Using Open Source iPhone UI Code
303 TANSTAAFL: Using Open Source iPhone UI Code303 TANSTAAFL: Using Open Source iPhone UI Code
303 TANSTAAFL: Using Open Source iPhone UI Code
 
YUI for control freaks - a presentation at The Ajax Experience
YUI for control freaks - a presentation at The Ajax ExperienceYUI for control freaks - a presentation at The Ajax Experience
YUI for control freaks - a presentation at The Ajax Experience
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
10 ways to make your code rock
10 ways to make your code rock10 ways to make your code rock
10 ways to make your code rock
 

Recently uploaded

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%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
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 

The Tale of a Smooth RecyclerView