SlideShare a Scribd company logo
1 of 37
Download to read offline
Recyclerview in Action
Kulina - PT Jejaring Makanan Indonesia
@pratamawijaya
#TechTalk8
9
./whoami
Pratama Nur Wijaya
Android Coder since 2012, usually active at GDG
Jogjakarta event, Yogyakarta Android Community and ID-
Android
Favorite Android Library : Square Library FTW!! and
RxAndroid X RxJava
Recomendation Blog: YKode, blog.danlew.net,
bignerdranch.com
Join our force
Send your cv and linkedin profile to pratama@kulina.id and may the
force be with u
Kulina is Hiring Android Developer
Recyclerview ?
“Recyclerview is a view group that
displays a list of scrollable items”
Listview
Why Google ?
- Listview codebase so complex
- Duplicate functionality
- Itemclicklistener vs onClickListener
- Hard to create animation
- And others (Google IO 2016)
https://www.youtube.com/watch?v=LqBlYJTfLP4
Why Google ?
Setup Recyclerview
dependencies {
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0' // recyclerview-v7
}
1. app/build.gradle
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
2. Layout
RecyclerView Component
RecyclerView
Layout Manager Item AnimatorAdapter Item Decoration
Positioning
View
Animating
View
Decorate
Item
Provide
View
Layout Manager
LinearLayoutManager
layoutManagerVertical = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManagerVertical);
layoutManagerHorizontal =
new LinearLayoutManager(context, LinearLayoutManager.
HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManagerHorizontal);
GridlayoutManager
gridLayoutManager = new GridLayoutManager(context, SPAN_COUNT);
recyclerView.setLayoutManager(gridLayoutManager);
StaggeredLayoutManager
gridLayoutManager = new StaggeredGridLayoutManager(2,
StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(gridLayoutManager);
Multitype Item Layout
Adapter
- Create View and Viewholder
- Bind item to ViewHolder
- Notify Recyclerview about changes
- Item Interaction handling (click, etc)
- Multiple view types
Adapter Component
public class LinearLayoutAdapter extends RecyclerView.Adapter<LinearLayoutAdapter.
LinearLayoutViewHolder>{
@Override public LinearLayoutViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override public void onBindViewHolder(LinearLayoutViewHolder holder, int position) {}
@Override public int getItemCount() {return 0;}
public class LinearLayoutViewHolder extends RecyclerView.ViewHolder{
public LinearLayoutViewHolder(View itemView) { super(itemView); }
}
}
Sample Adapter Clas
Viewholder Class
public class LinearHolder extends RecyclerView.ViewHolder {
@BindView(R.id.img) ImageView img;
@BindView(R.id.name) TextView name;
@BindView(R.id.location) TextView location;
public LinearHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bindItem(Mountain mountain) {
name.setText(mountain.name);
location.setText(mountain.location);
ImageLoader.loadImage(context, img, mountain.img);
}
}
Create View and Bind View
@Override public LinearHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new LinearHolder(
LayoutInflater.from(context).inflate(R.layout.item_linear_vertical, parent, false));
}
@Override public void onBindViewHolder(LinearHolder holder, int position) {
if (mountainList != null && mountainList.size() > 0) {
holder.bindItem(mountainList.get(position));
}
}
Handle Click Listener
Create interface
public interface ClickListener {
void onItemClick(int pos);
}
Pass Listener into Adapter
public interface ClickListener {
void onItemClick(int pos);
}
private Context context;
private List<String> strings;
private ClickListener listener;
public TextAdapter(Context context, List<String> strings, ClickListener listener) {
this.context = context;
this.strings = strings;
this.listener = listener;
}
Set Clicklistener at ViewHolder
public class MainHolder extends RecyclerView.ViewHolder {
@BindView(R.id.txt_title) TextView txtTitle;
@BindView(R.id.container_text) LinearLayout container;
public MainHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bindData(String string, final int pos) {
txtTitle.setText("" + string);
container.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
listener.onItemClick(pos);
}
});
}
}
Implement Listener into Activity/Fragment
public class HomeFragment extends Fragment implements TextAdapter.ClickListener{
// rest fragment class
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
adapter = new TextAdapter(getActivity(), listMenu, this);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
@Override public void onItemClick(int pos) {
// do something
}
}
Item Decoration
Simple Item Decoration
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public SimpleDividerItemDecoration(Context context) {
mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
}
@Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}}}
Simple Item Decoration
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
Item Animator
Simple Item Animator
recyclerView.setItemAnimator(new DefaultItemAnimator());
Other item animator ?
Take a look
https://github.com/wasabeef/recyclerview-animators ?;)
Multitype Viewholder
Multiviewtype Holder
@Override public int getItemViewType(int position) {
if (position == 0) {
return VIEW_TYPE_HEADER;
} else {
return VIEW_TYPE_ITEM;
}
}
@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder.getItemViewType() == VIEW_TYPE_HEADER) {
HeaderAdapterHolder headerHolder = (HeaderAdapterHolder) holder;
}else if (holder.getItemViewType() == VIEW_TYPE_ITEM) {
ItemAdapterHolder itemHolder = (ItemAdapterHolder) holder;
}
}
http://hannesdorfmann.com/android/adapter-delegates
https://github.com/sockeqwe/AdapterDelegates
Do you found adapter hell ?
Take a look these awesome article
Reference
- Dave Smith ~ Mastering Recyclerview http://www.slideshare.
net/devunwired/mastering-recyclerview-layouts
- Google I/O 2016 ~ https://www.youtube.com/watch?v=LqBlYJTfLP4
- BignerdRanch https://www.bignerdranch.com/blog/recyclerview-part-1-fundamentals-
for-listview-experts/
- http://hannesdorfmann.com/android/adapter-delegates
- May Google be with you
Thanks

More Related Content

What's hot (20)

Kotlin on android
Kotlin on androidKotlin on android
Kotlin on android
 
Javascript
JavascriptJavascript
Javascript
 
React js
React jsReact js
React js
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Jetpack Compose.pptx
Jetpack Compose.pptxJetpack Compose.pptx
Jetpack Compose.pptx
 
React for Beginners
React for BeginnersReact for Beginners
React for Beginners
 
React hooks
React hooksReact hooks
React hooks
 
Jetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UIJetpack Compose - Android’s modern toolkit for building native UI
Jetpack Compose - Android’s modern toolkit for building native UI
 
GraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer toolsGraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer tools
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
React js for beginners
React js for beginnersReact js for beginners
React js for beginners
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
ReactJS
ReactJSReactJS
ReactJS
 
Kotlin Coroutines - the new async
Kotlin Coroutines - the new asyncKotlin Coroutines - the new async
Kotlin Coroutines - the new async
 
Jetpack Compose beginner.pdf
Jetpack Compose beginner.pdfJetpack Compose beginner.pdf
Jetpack Compose beginner.pdf
 
React workshop presentation
React workshop presentationReact workshop presentation
React workshop presentation
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
Fragment
Fragment Fragment
Fragment
 
JavaScript Promises
JavaScript PromisesJavaScript Promises
JavaScript Promises
 
Core java
Core javaCore java
Core java
 

Viewers also liked

Tech Talk #2: Android app performance tips
Tech Talk #2: Android app performance tipsTech Talk #2: Android app performance tips
Tech Talk #2: Android app performance tipsNexus FrontierTech
 
More Android UIs
More Android UIsMore Android UIs
More Android UIsDENNIS JUNG
 
Infinum Android Talks #09 - UI optimization
Infinum Android Talks #09 - UI optimization Infinum Android Talks #09 - UI optimization
Infinum Android Talks #09 - UI optimization Infinum
 
Android UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesAndroid UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesMarakana Inc.
 
Build and Distributing SDK Add-Ons
Build and Distributing SDK Add-OnsBuild and Distributing SDK Add-Ons
Build and Distributing SDK Add-OnsDave Smith
 
ExtraLayoutSpace of RecyclerView
ExtraLayoutSpace of RecyclerViewExtraLayoutSpace of RecyclerView
ExtraLayoutSpace of RecyclerViewMima Yuki
 
Write cleaner, maintainable, and testable code in Android with MVVM
Write cleaner, maintainable, and testable code in Android with MVVMWrite cleaner, maintainable, and testable code in Android with MVVM
Write cleaner, maintainable, and testable code in Android with MVVMAdil Mughal
 
Android 02 - Recycler View Adapter
Android 02 - Recycler View AdapterAndroid 02 - Recycler View Adapter
Android 02 - Recycler View AdapterAline Borges
 
Android Training (AdapterView & Adapter)
Android Training (AdapterView & Adapter)Android Training (AdapterView & Adapter)
Android Training (AdapterView & Adapter)Khaled Anaqwa
 
Tutorial android - créer des apps
Tutorial android - créer des appsTutorial android - créer des apps
Tutorial android - créer des appsNoé Breiss
 
Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2
Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2
Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2Mathias Seguy
 
droidgirls Recyclerview
droidgirls Recyclerviewdroidgirls Recyclerview
droidgirls RecyclerviewYuki Anzai
 
Master of RecyclerView
Master of RecyclerViewMaster of RecyclerView
Master of RecyclerViewYuki Anzai
 
Android-Tp2: liste et adaptateurs
Android-Tp2: liste et adaptateursAndroid-Tp2: liste et adaptateurs
Android-Tp2: liste et adaptateursLilia Sfaxi
 
Beauty Treatment for your Android Application
Beauty Treatment for your Android ApplicationBeauty Treatment for your Android Application
Beauty Treatment for your Android ApplicationCodemotion
 

Viewers also liked (20)

Tech Talk #2: Android app performance tips
Tech Talk #2: Android app performance tipsTech Talk #2: Android app performance tips
Tech Talk #2: Android app performance tips
 
More Android UIs
More Android UIsMore Android UIs
More Android UIs
 
Infinum Android Talks #09 - UI optimization
Infinum Android Talks #09 - UI optimization Infinum Android Talks #09 - UI optimization
Infinum Android Talks #09 - UI optimization
 
Android UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesAndroid UI Tips, Tricks and Techniques
Android UI Tips, Tricks and Techniques
 
Build and Distributing SDK Add-Ons
Build and Distributing SDK Add-OnsBuild and Distributing SDK Add-Ons
Build and Distributing SDK Add-Ons
 
ExtraLayoutSpace of RecyclerView
ExtraLayoutSpace of RecyclerViewExtraLayoutSpace of RecyclerView
ExtraLayoutSpace of RecyclerView
 
Write cleaner, maintainable, and testable code in Android with MVVM
Write cleaner, maintainable, and testable code in Android with MVVMWrite cleaner, maintainable, and testable code in Android with MVVM
Write cleaner, maintainable, and testable code in Android with MVVM
 
Fun with RecyclerView
Fun with RecyclerViewFun with RecyclerView
Fun with RecyclerView
 
Bonnes pratiques développement android
Bonnes pratiques développement androidBonnes pratiques développement android
Bonnes pratiques développement android
 
Android 02 - Recycler View Adapter
Android 02 - Recycler View AdapterAndroid 02 - Recycler View Adapter
Android 02 - Recycler View Adapter
 
Android Training (AdapterView & Adapter)
Android Training (AdapterView & Adapter)Android Training (AdapterView & Adapter)
Android Training (AdapterView & Adapter)
 
Tutorial android - créer des apps
Tutorial android - créer des appsTutorial android - créer des apps
Tutorial android - créer des apps
 
Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2
Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2
Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2
 
Android adapters
Android adaptersAndroid adapters
Android adapters
 
droidgirls Recyclerview
droidgirls Recyclerviewdroidgirls Recyclerview
droidgirls Recyclerview
 
Master of RecyclerView
Master of RecyclerViewMaster of RecyclerView
Master of RecyclerView
 
Hipertencion expo
Hipertencion expoHipertencion expo
Hipertencion expo
 
Android-Tp2: liste et adaptateurs
Android-Tp2: liste et adaptateursAndroid-Tp2: liste et adaptateurs
Android-Tp2: liste et adaptateurs
 
Android Material Design
Android Material DesignAndroid Material Design
Android Material Design
 
Beauty Treatment for your Android Application
Beauty Treatment for your Android ApplicationBeauty Treatment for your Android Application
Beauty Treatment for your Android Application
 

Similar to Recyclerview in action

Building Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture ComponentsBuilding Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture ComponentsHassan Abid
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureAlexey Buzdin
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureC.T.Co
 
안드로이드 데이터 바인딩
안드로이드 데이터 바인딩안드로이드 데이터 바인딩
안드로이드 데이터 바인딩GDG Korea
 
Saindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidSaindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidDaniel Baccin
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfShaiAlmog1
 
Android Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation DrawerAndroid Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation DrawerAgus Haryanto
 
07_UIAndroid.pdf
07_UIAndroid.pdf07_UIAndroid.pdf
07_UIAndroid.pdfImranS18
 
Android dev toolbox
Android dev toolboxAndroid dev toolbox
Android dev toolboxShem Magnezi
 
Architecture components - IT Talk
Architecture components - IT TalkArchitecture components - IT Talk
Architecture components - IT TalkConstantine Mars
 
Architecture Components
Architecture Components Architecture Components
Architecture Components DataArt
 
Nicola Corti - Building UI Consistent Android Apps - Codemotion Milan 2017
Nicola Corti - Building UI Consistent Android Apps - Codemotion Milan 2017Nicola Corti - Building UI Consistent Android Apps - Codemotion Milan 2017
Nicola Corti - Building UI Consistent Android Apps - Codemotion Milan 2017Codemotion
 
Advanced #2 networking
Advanced #2   networkingAdvanced #2   networking
Advanced #2 networkingVitali Pekelis
 
ThingMaker in Swift
ThingMaker in SwiftThingMaker in Swift
ThingMaker in Swiftallanh0526
 
Android architecture component - FbCircleDev Yogyakarta Indonesia
Android architecture component - FbCircleDev Yogyakarta IndonesiaAndroid architecture component - FbCircleDev Yogyakarta Indonesia
Android architecture component - FbCircleDev Yogyakarta IndonesiaPratama Nur Wijaya
 
Taming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeTaming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeMacoscope
 
Embracing the Lollipop
Embracing the LollipopEmbracing the Lollipop
Embracing the LollipopSonja Kesic
 

Similar to Recyclerview in action (20)

Building Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture ComponentsBuilding Modern Apps using Android Architecture Components
Building Modern Apps using Android Architecture Components
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
안드로이드 데이터 바인딩
안드로이드 데이터 바인딩안드로이드 데이터 바인딩
안드로이드 데이터 바인딩
 
Saindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidSaindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender android
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
 
ButterKnife
ButterKnifeButterKnife
ButterKnife
 
Android Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation DrawerAndroid Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation Drawer
 
07_UIAndroid.pdf
07_UIAndroid.pdf07_UIAndroid.pdf
07_UIAndroid.pdf
 
Android dev toolbox
Android dev toolboxAndroid dev toolbox
Android dev toolbox
 
Architecture components - IT Talk
Architecture components - IT TalkArchitecture components - IT Talk
Architecture components - IT Talk
 
Architecture Components
Architecture Components Architecture Components
Architecture Components
 
Android crashcourse
Android crashcourseAndroid crashcourse
Android crashcourse
 
Nicola Corti - Building UI Consistent Android Apps - Codemotion Milan 2017
Nicola Corti - Building UI Consistent Android Apps - Codemotion Milan 2017Nicola Corti - Building UI Consistent Android Apps - Codemotion Milan 2017
Nicola Corti - Building UI Consistent Android Apps - Codemotion Milan 2017
 
Advanced #2 networking
Advanced #2   networkingAdvanced #2   networking
Advanced #2 networking
 
ThingMaker in Swift
ThingMaker in SwiftThingMaker in Swift
ThingMaker in Swift
 
Android architecture component - FbCircleDev Yogyakarta Indonesia
Android architecture component - FbCircleDev Yogyakarta IndonesiaAndroid architecture component - FbCircleDev Yogyakarta Indonesia
Android architecture component - FbCircleDev Yogyakarta Indonesia
 
Taming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeTaming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, Macoscope
 
Android development
Android developmentAndroid development
Android development
 
Embracing the Lollipop
Embracing the LollipopEmbracing the Lollipop
Embracing the Lollipop
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Recently uploaded (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Recyclerview in action

  • 1. Recyclerview in Action Kulina - PT Jejaring Makanan Indonesia @pratamawijaya #TechTalk8 9
  • 2. ./whoami Pratama Nur Wijaya Android Coder since 2012, usually active at GDG Jogjakarta event, Yogyakarta Android Community and ID- Android Favorite Android Library : Square Library FTW!! and RxAndroid X RxJava Recomendation Blog: YKode, blog.danlew.net, bignerdranch.com
  • 4. Send your cv and linkedin profile to pratama@kulina.id and may the force be with u Kulina is Hiring Android Developer
  • 6. “Recyclerview is a view group that displays a list of scrollable items”
  • 9. - Listview codebase so complex - Duplicate functionality - Itemclicklistener vs onClickListener - Hard to create animation - And others (Google IO 2016) https://www.youtube.com/watch?v=LqBlYJTfLP4 Why Google ?
  • 10. Setup Recyclerview dependencies { compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:design:23.4.0' // recyclerview-v7 } 1. app/build.gradle <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" /> 2. Layout
  • 11. RecyclerView Component RecyclerView Layout Manager Item AnimatorAdapter Item Decoration Positioning View Animating View Decorate Item Provide View
  • 13. LinearLayoutManager layoutManagerVertical = new LinearLayoutManager(context); recyclerView.setLayoutManager(layoutManagerVertical); layoutManagerHorizontal = new LinearLayoutManager(context, LinearLayoutManager. HORIZONTAL, false); recyclerView.setLayoutManager(layoutManagerHorizontal);
  • 14. GridlayoutManager gridLayoutManager = new GridLayoutManager(context, SPAN_COUNT); recyclerView.setLayoutManager(gridLayoutManager);
  • 15. StaggeredLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(gridLayoutManager);
  • 18. - Create View and Viewholder - Bind item to ViewHolder - Notify Recyclerview about changes - Item Interaction handling (click, etc) - Multiple view types Adapter Component
  • 19. public class LinearLayoutAdapter extends RecyclerView.Adapter<LinearLayoutAdapter. LinearLayoutViewHolder>{ @Override public LinearLayoutViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return null; } @Override public void onBindViewHolder(LinearLayoutViewHolder holder, int position) {} @Override public int getItemCount() {return 0;} public class LinearLayoutViewHolder extends RecyclerView.ViewHolder{ public LinearLayoutViewHolder(View itemView) { super(itemView); } } } Sample Adapter Clas
  • 20. Viewholder Class public class LinearHolder extends RecyclerView.ViewHolder { @BindView(R.id.img) ImageView img; @BindView(R.id.name) TextView name; @BindView(R.id.location) TextView location; public LinearHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } public void bindItem(Mountain mountain) { name.setText(mountain.name); location.setText(mountain.location); ImageLoader.loadImage(context, img, mountain.img); } }
  • 21. Create View and Bind View @Override public LinearHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new LinearHolder( LayoutInflater.from(context).inflate(R.layout.item_linear_vertical, parent, false)); } @Override public void onBindViewHolder(LinearHolder holder, int position) { if (mountainList != null && mountainList.size() > 0) { holder.bindItem(mountainList.get(position)); } }
  • 23. Create interface public interface ClickListener { void onItemClick(int pos); }
  • 24. Pass Listener into Adapter public interface ClickListener { void onItemClick(int pos); } private Context context; private List<String> strings; private ClickListener listener; public TextAdapter(Context context, List<String> strings, ClickListener listener) { this.context = context; this.strings = strings; this.listener = listener; }
  • 25. Set Clicklistener at ViewHolder public class MainHolder extends RecyclerView.ViewHolder { @BindView(R.id.txt_title) TextView txtTitle; @BindView(R.id.container_text) LinearLayout container; public MainHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } public void bindData(String string, final int pos) { txtTitle.setText("" + string); container.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.onItemClick(pos); } }); } }
  • 26. Implement Listener into Activity/Fragment public class HomeFragment extends Fragment implements TextAdapter.ClickListener{ // rest fragment class @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ButterKnife.bind(this, view); adapter = new TextAdapter(getActivity(), listMenu, this); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setAdapter(adapter); } @Override public void onItemClick(int pos) { // do something } }
  • 28. Simple Item Decoration public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration { private Drawable mDivider; public SimpleDividerItemDecoration(Context context) { mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider); } @Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { int left = parent.getPaddingLeft(); int right = parent.getWidth() - parent.getPaddingRight(); int childCount = parent.getChildCount(); for (int i = 0; i < childCount; i++) { View child = parent.getChildAt(i); RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); int top = child.getBottom() + params.bottomMargin; int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); }}}
  • 29. Simple Item Decoration recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
  • 32. Other item animator ? Take a look https://github.com/wasabeef/recyclerview-animators ?;)
  • 34. Multiviewtype Holder @Override public int getItemViewType(int position) { if (position == 0) { return VIEW_TYPE_HEADER; } else { return VIEW_TYPE_ITEM; } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder.getItemViewType() == VIEW_TYPE_HEADER) { HeaderAdapterHolder headerHolder = (HeaderAdapterHolder) holder; }else if (holder.getItemViewType() == VIEW_TYPE_ITEM) { ItemAdapterHolder itemHolder = (ItemAdapterHolder) holder; } }
  • 36. Reference - Dave Smith ~ Mastering Recyclerview http://www.slideshare. net/devunwired/mastering-recyclerview-layouts - Google I/O 2016 ~ https://www.youtube.com/watch?v=LqBlYJTfLP4 - BignerdRanch https://www.bignerdranch.com/blog/recyclerview-part-1-fundamentals- for-listview-experts/ - http://hannesdorfmann.com/android/adapter-delegates - May Google be with you