Skip to content

Instantly share code, notes, and snippets.

View magdamiu's full-sized avatar

Magda Miu magdamiu

View GitHub Profile
@magdamiu
magdamiu / EmailAdapter.java
Created December 28, 2020 21:45
Update the Adapter
// Clean all elements of the recycler
public void clear() {
emails.clear();
notifyDataSetChanged();
}
// Add a list of items
public void addAll(List<Email> list) {
emails.addAll(list);
notifyDataSetChanged();
@magdamiu
magdamiu / main_layout.xml
Created December 28, 2020 21:42
Wrap RecyclerView in a SwipeRefreshLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewEmails"
android:layout_width="match_parent"
android:layout_height="match_parent" />
@magdamiu
magdamiu / build.gradle
Created December 28, 2020 21:41
Add swiperefreshlayout dependency
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
@magdamiu
magdamiu / MainActivity.java
Created December 28, 2020 21:33
Set an item decorator
private void setItemDecorator() {
RecyclerView.ItemDecoration itemDecoration = new
DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
recyclerViewEmails.addItemDecoration(itemDecoration);
}
@magdamiu
magdamiu / EmailDiffCallback.java
Created December 28, 2020 21:24
EmailDiffCallback for fast rendering
public class EmailDiffCallback extends DiffUtil.Callback {
private List<Email> oldList;
private List<Email> newList;
public EmailDiffCallback(List<Email> oldList, List<Email> newList) {
this.oldList = oldList;
this.newList = newList;
}
@magdamiu
magdamiu / MainActivity.java
Created December 28, 2020 21:23
Slow rendering best practices
// NOT OK
void onNewEmailsArrivedNotRecommended(List<Email> newEmails) {
emailAdapter.setEmails(newEmails);
emailAdapter.notifyDataSetChanged();
}
// OK
void onNewDataArrivedFastRendering(List<Email> newEmails) {
List<Email> oldEmails = emailAdapter.getEmails();
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new EmailDiffCallback(oldEmails, newEmails));
@magdamiu
magdamiu / MainActivity.java
Created December 28, 2020 20:17
Combine all the code
private void setEmailsAdapter() {
recyclerViewEmails.setAdapter(new EmailAdapter(this, emails));
}
private void displayEmailsList() {
// data source - checked
inbox();
// layout manager - checked
setEmailsLayoutManager();
@magdamiu
magdamiu / EmailAdapter.java
Created December 28, 2020 20:16
The custom adapter for emails list
public class EmailAdapter extends RecyclerView.Adapter<EmailViewHolder> {
private List<Email> emails;
private Context context;
public EmailAdapter(Context context, List<Email> emails) {
this.emails = emails;
this.context = context;
}
// creates the items and add them to the RecyclerView, just the layout
@magdamiu
magdamiu / EmailViewHolder.java
Created December 28, 2020 20:14
Define the custom ViewHolder for our emails list.
public class EmailViewHolder extends RecyclerView.ViewHolder{
private final TextView textViewFrom, textViewSubject, textViewBody;
private final LinearLayout linearLayoutEmail;
public EmailViewHolder(@NonNull View itemView) {
super(itemView);
textViewFrom = itemView.findViewById(R.id.textViewFrom);
textViewSubject = itemView.findViewById(R.id.textViewSubject);
@magdamiu
magdamiu / MainActivity.java
Created December 28, 2020 20:13
Define the LayoutManager, in our case we will use the linear one
private void setEmailsLayoutManager() {
recyclerViewEmails.setLayoutManager(new LinearLayoutManager(this));
}