recyclerView.addOnScrollListener - "retrofit pagination with MVVM" is loading the same response/list
After hundreds of tries, I finally solved it, here's the solution of problem
Firstly I changed the GET
method in the API PostInterface
and make it take @URL
instead of @Query
KEY like this
public interface PostInterface {
@GET
Call<PostList> getPostList(@Url String URL);
}
Secondary I edited the PostsClient
removed final from BASE_URL
private static String BASE_URL
and create a setter & getter for BASE URL & KEY
public static String getKEY() {
return KEY;
}
public static String getBaseUrl() {
return BASE_URL;
}
Thirdly & finally I moved this if statement for the token checker after the response
public void getPosts(){
Log.e(TAG,finalURL.getValue());
PostsClient.getINSTANCE().getPostList(finalURL.getValue()).enqueue(new Callback<PostList>() {
@Override
public void onResponse(@NotNull Call<PostList> call, @NotNull Response<PostList> response) {
PostList list = response.body();
if (list.getItems() != null) {
Log.e(TAG,list.getNextPageToken());
token.setValue(list.getNextPageToken());
postListMutableLiveData.setValue(list);
}
if (token.getValue() == null || !token.getValue().equals("") ) {
finalURL.setValue(finalURL.getValue() + "&pageToken=" + token.getValue());
}
// Log.i(TAG,response.body().getItems().toString());
}
@Override
public void onFailure(Call<PostList> call, Throwable t) {
Log.e(TAG,t.getMessage());
}
});
}
Consider using the new paging library. Here's a sample and the documentation. It performs better and you don't have to manually keep track where you are in the scrolling. By following the sample I found it fairly easy to implement this, although it is a lot of boilerplate code.## Heading ##