Skip to content

Commit

Permalink
L18: Step 7. Make the RecyclerView Adapter work with a PagedList
Browse files Browse the repository at this point in the history
  • Loading branch information
AOrobator committed Apr 5, 2019
1 parent 32da265 commit d59eb74
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
12 changes: 12 additions & 0 deletions lesson18/Lesson18_Paging.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ In `GithubRepository.search()` method, make the following changes:
return new RepoSearchResult(data, networkErrors);
}
```

## 7. Make the RecyclerView Adapter work with a PagedList
To bind a `PagedList` to a `RecycleView`, use a [PagedListAdapter]. The `PagedListAdapter` gets
notified whenever the `PagedList` content is loaded and then signals the `RecyclerView` to update.

**Update the `ReposAdapter` to work with a `PagedList`:**

* Right now, `ReposAdapter` is a `ListAdapter`. Make it a `PagedListAdapter`:
```java
class ReposAdapter extends PagedListAdapter<Repo, RecyclerView.ViewHolder>
```
Our app finally compiles! Run it, and check out how it works.

[Paging library]: https://developer.android.com/topic/libraries/architecture/paging
[Paging Codelab]: https://codelabs.developers.google.com/codelabs/android-paging/index.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.orobator.helloandroid.lesson18.model.Repo;

/**
* Adapter for the list of repositories
*/
class ReposAdapter extends ListAdapter<Repo, RecyclerView.ViewHolder> {
class ReposAdapter extends PagedListAdapter<Repo, RecyclerView.ViewHolder> {
private static final DiffUtil.ItemCallback<Repo> REPO_COMPARATOR =
new DiffUtil.ItemCallback<Repo>() {
@Override public boolean areItemsTheSame(@NonNull Repo oldItem, @NonNull Repo newItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void initAdapter() {
if (repos != null) {
size = repos.size();
}
Log.d("Activity", "list: " + size + "");
Log.d(SearchRepositoriesActivity.class.getSimpleName(), "list: " + size + "");
showEmptyList(size == 0);
adapter.submitList(repos);
});
Expand Down Expand Up @@ -106,6 +106,7 @@ private void initSearch(String query) {
private void updateRepoListFromInput() {
String input = searchRepoEditText.getText().toString().trim();
if (!TextUtils.isEmpty(input)) {
Log.d(SearchRepositoriesActivity.class.getSimpleName(), "Got input: " + input);
list.scrollToPosition(0);
viewModel.searchRepo(input);
adapter.submitList(null);
Expand Down

0 comments on commit d59eb74

Please sign in to comment.