SlideShare a Scribd company logo
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

PDF
Mastering RecyclerView Layouts
PPTX
Recycler view
PPTX
ListView RecyclerView.pptx
PPTX
Visitor design pattern
PDF
Declarative UIs with Jetpack Compose
PDF
JVM Memory Management Details
PPT
Java collection
PPTX
React Hooks
Mastering RecyclerView Layouts
Recycler view
ListView RecyclerView.pptx
Visitor design pattern
Declarative UIs with Jetpack Compose
JVM Memory Management Details
Java collection
React Hooks

What's hot (20)

PPT
Java collections concept
PPTX
Java 8 - Features Overview
PPTX
Spring boot
PDF
Kotlin for Android Development
PPT
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
PDF
Collections In Java
PDF
Try Jetpack Compose
PDF
07 java collection
PDF
Introducing Clean Architecture
PPTX
Solid principles
PDF
Angular directives and pipes
PDF
Zero-Downtime Deployment with Kubernetes, SpringBoot & Flyway
PDF
Kubernetes device plugins
PPTX
Optional in Java 8
PPTX
Factory Design Pattern
PPTX
PDF
Clean Architecture
PPSX
Collections - Maps
PPTX
Collections framework in java
PDF
Nouveautés Java 9-10-11
Java collections concept
Java 8 - Features Overview
Spring boot
Kotlin for Android Development
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
Collections In Java
Try Jetpack Compose
07 java collection
Introducing Clean Architecture
Solid principles
Angular directives and pipes
Zero-Downtime Deployment with Kubernetes, SpringBoot & Flyway
Kubernetes device plugins
Optional in Java 8
Factory Design Pattern
Clean Architecture
Collections - Maps
Collections framework in java
Nouveautés Java 9-10-11
Ad

Viewers also liked (20)

PDF
Tech Talk #2: Android app performance tips
PPTX
More Android UIs
PDF
Infinum Android Talks #09 - UI optimization
PDF
Android UI Tips, Tricks and Techniques
PDF
Build and Distributing SDK Add-Ons
PDF
ExtraLayoutSpace of RecyclerView
PDF
Write cleaner, maintainable, and testable code in Android with MVVM
PPTX
Fun with RecyclerView
PDF
Bonnes pratiques développement android
PPTX
Android 02 - Recycler View Adapter
PPTX
Android Training (AdapterView & Adapter)
PDF
Tutorial android - créer des apps
PPTX
Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2
PDF
droidgirls Recyclerview
PDF
Master of RecyclerView
PPTX
Hipertencion expo
PDF
Android-Tp2: liste et adaptateurs
POT
Android Material Design
PDF
Beauty Treatment for your Android Application
Tech Talk #2: Android app performance tips
More Android UIs
Infinum Android Talks #09 - UI optimization
Android UI Tips, Tricks and Techniques
Build and Distributing SDK Add-Ons
ExtraLayoutSpace of RecyclerView
Write cleaner, maintainable, and testable code in Android with MVVM
Fun with RecyclerView
Bonnes pratiques développement android
Android 02 - Recycler View Adapter
Android Training (AdapterView & Adapter)
Tutorial android - créer des apps
Architecture et Bonnes pratiques Android #DevoxxFr2016 Part2
droidgirls Recyclerview
Master of RecyclerView
Hipertencion expo
Android-Tp2: liste et adaptateurs
Android Material Design
Beauty Treatment for your Android Application
Ad

Similar to Recyclerview in action (20)

PDF
Volley lab btc_bbit
PDF
Deep Dive Into LayoutManager for RecyclerView
PDF
ButterKnife
PDF
Getting Started With Material Design
PPTX
Binding data with the AdapterView class.pptx
PPTX
Introdução ao RecyclerView
PDF
Android app material design from dev's perspective
PPTX
Gdg cabreuva #06 recycler view no android
PDF
Android Materials Design
PDF
Material Design (The Technical Essentials) by Mohammad Aljobairi @AMMxDROID
PDF
Oleksandr Tolstykh
PDF
How to use data binding in android
PDF
Dinosaurs and Androids: The Listview Evolution
PDF
RxBinding-kotlin
PPTX
Embracing the Lollipop
PPTX
Recycler mvp
PPTX
Android 3
PDF
Material Design and Backwards Compatibility
PDF
Android Lollipop - Webinar am 11.12.2014
PDF
The Tale of a Smooth RecyclerView
Volley lab btc_bbit
Deep Dive Into LayoutManager for RecyclerView
ButterKnife
Getting Started With Material Design
Binding data with the AdapterView class.pptx
Introdução ao RecyclerView
Android app material design from dev's perspective
Gdg cabreuva #06 recycler view no android
Android Materials Design
Material Design (The Technical Essentials) by Mohammad Aljobairi @AMMxDROID
Oleksandr Tolstykh
How to use data binding in android
Dinosaurs and Androids: The Listview Evolution
RxBinding-kotlin
Embracing the Lollipop
Recycler mvp
Android 3
Material Design and Backwards Compatibility
Android Lollipop - Webinar am 11.12.2014
The Tale of a Smooth RecyclerView

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation theory and applications.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation_ Review paper, used for researhc scholars
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Building Integrated photovoltaic BIPV_UPV.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Empathic Computing: Creating Shared Understanding
Encapsulation theory and applications.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
20250228 LYD VKU AI Blended-Learning.pptx
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?

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