This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void setItemDecorator() { | |
RecyclerView.ItemDecoration itemDecoration = new | |
DividerItemDecoration(this, DividerItemDecoration.VERTICAL); | |
recyclerViewEmails.addItemDecoration(itemDecoration); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void setEmailsAdapter() { | |
recyclerViewEmails.setAdapter(new EmailAdapter(this, emails)); | |
} | |
private void displayEmailsList() { | |
// data source - checked | |
inbox(); | |
// layout manager - checked | |
setEmailsLayoutManager(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void setEmailsLayoutManager() { | |
recyclerViewEmails.setLayoutManager(new LinearLayoutManager(this)); | |
} |