android-paging | Jetpack Paging codelab | Model View Controller library
kandi X-RAY | android-paging Summary
kandi X-RAY | android-paging Summary
Jetpack Paging codelab
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of android-paging
android-paging Key Features
android-paging Examples and Code Snippets
Community Discussions
Trending Discussions on android-paging
QUESTION
Hello Guys im using Android Jetpack Paging library 3, I'm creating a news app that implements network + database scenario, and im following the codelab by google https://codelabs.developers.google.com/codelabs/android-paging , im doing it almost like in the codelab i almost matched all the operations shown in the examples https://github.com/android/architecture-components-samples/tree/main/PagingWithNetworkSample.
It works almost as it should...but my backend response is page keyed, i mean response comes with the list of news and the next page url, remote mediator fetches the data, populates the database, repository is set, viewmodel is set...
The problem is : when recyclerview loads the data , following happens:recyclerview flickers, items jump, are removed , added again and so on. I dont know why recyclerview or its itemanimator behaves like that , that looks so ugly and glitchy. More than that, when i scroll to the end of the list new items are fetched and that glitchy and jumping effect is happening again.
I would be very grateful if you could help me, im sitting on it for three days , thank you very much in advance.Here are my code snippets:
...ANSWER
Answered 2021-Mar-19 at 18:15Just following up here from comments:
Setting pageSize = 10
fixes the issue.
The issue was with pageSize
being too small, resulting in PagingSource
refreshes loading pages that did not cover the viewport. Since source refresh replaces the list and goes through DiffUtil
, you need to provide an initialLoadSize
that is large enough so that there is some overlap (otherwise scroll position will be lost).
BTW - Paging loads additional data automatically based on PagingConfig.prefetchDistance
. If RecyclerView
binds items close enough to the edge of the list, it will automatically trigger APPEND / PREPEND loads. This is why the default of initialLoadSize
is 3 * pageSize
, but if you're still experiencing additional loads, I would suggest either adjusting prefetchDistance, or increasing initialLoadSize
further.
QUESTION
Following this codelab I have implemented pagination and caching using RemoteMediator
(I am calling a simple API that returns a list of news). I am injecting this RemoteMediator
to Repository which has method getResultStream()
that returns Flow>
. In ViewModel is function getNews() : Flow>
and in Fragment I am calling this function and submitting list to RecyclerAdapter.
Now I want to add a new API call that returns news but with search keyword. What is the proper way to do it? Do I have to write all this code again and create a new RemoteMediator
? The logic will be the same but now I have to pass a string argument to the Retrofit get function. The result of this call will replace items in RecyclerView, so I will have two sources of data but one Recycler, do I have to create also a MediatorLiveData
? (I don't add any code but if it help I can do it)
One person asked how exactly I did it (Question posted as answer so it is deleted now but maybe it will help someone in future). So, in ViewModel I have this:
...ANSWER
Answered 2020-Nov-15 at 19:24Assuming you still have one recyclerview, you can do something like:
QUESTION
After making a simple app with Paging 3 (based on this Google GitHub codelab) my app crashes. When I scroll down, at some point (probably when the new GET function is called). Logcat looks like this:
...ANSWER
Answered 2020-Nov-16 at 19:18I am not sure but I think it was just a bug in the library. I was using 3.0.0-alpha08
but after updating to 3.0.0-alpha09 I don't get any errors. I tested it in my app and in Google sample project after updating the version and it is working.
Fix for
IllegalArgumentException
being throw when using separators withRemoteMediator
and an invalidate is triggered while a remote load that would returnendOfPagination
is still running (I3a260)
QUESTION
How to convert this Kotlin class to Java
...ANSWER
Answered 2020-Oct-27 at 02:34Decompiling that class to Java with IntelliJ produces this. I changed Repo
to String
so that it would compile locally without dependencies.
QUESTION
ANSWER
Answered 2020-Jun-21 at 09:44I solved it. this is my code
In your Activity
QUESTION
Is background threading managed automatically with PagingData
as it is with PagedList
, and then returned on the main thread?
From the logs below, it appears PagingData
is not run on the backgroud thread in the Paging 3 library compared to PagedList
in Paging 2's library.
Expect (Based on the Paging Codelab sample)
- GithubPagingSource
override suspend fun load(...)
to run on an IO thread. - SearchRepositoriesActivity
viewModel.searchRepo(query).collectLatest { ... }
to run on the main thread.
Observe
- Both GithubPagingSource
override suspend fun load(...)
and SearchRepositoriesActivityviewModel.searchRepo(query).collectLatest { ... }
run on the main thread.
Threading is handled on the background by PagedList
with toLiveData
according to the documentation.
Paging 3If you use LivePagedListBuilder to get a LiveData, it will initialize PagedLists on a background thread for you.
The Paging 3 documentation does not mention how threading is managed. However, from the logs, PagingSource
appears to be running the network request on a main thread and returning the PagingData
on a main thread.
I've recreated the Codelab pattern in the CryptoTweets sample app app-simple module.
FeedPagingSource.kt
...ANSWER
Answered 2020-Sep-21 at 04:26When implementing PagingData
and PagingSource
with Kotlin Coroutines and making a Retrofit network request, as in the sample code above, Retrofit handles the background threading by default and returns on the main thread.
See StackOverflow: Does Retrofit make network calls on main thread?
RxJavaWhen using RxJava with Retrofit as the network source, the threading needs to be explicitly specified as shown in the Android documentation sample.
See Android documentation: Page from network and database > Implement a RemoteMediator
QUESTION
I'm currently using Google's Paging3 library for the first time. I'm basing my code on their Codelab at https://codelabs.developers.google.com/codelabs/android-paging/#0.
The flow of my app adheres to the following rules:
- display empty state
- let users search for something
- display results
- when user presses back button on searchbar -> display empty state again
The codelab unfortunately does not deal with empty states :( I'm using the following addLoadStateListener to deal with the search results:
...ANSWER
Answered 2020-Sep-18 at 06:35Here are a couple APIs you might find helpful:
You can use PagingData.empty()
to create an empty PagingData
you can submit to "clear" the previous PagingData
, until you call adapter.submitData
again. This is useful if you want to show empty state between searches or before searching. You can also use PagingData.from(list)
if you want to show some static content.
To get the presented item count you can use adapter.itemCount
If you need to check specifically what items are loaded after refresh finishes, you can use adapter.peek(index)
or adapter.snapshot()
to get all the items at once (but is more expensive).
QUESTION
I was trying to imitate Google's codelab for the new Paging 3 library, and I encountered the following error when I tried to have a Room DAO method return a PagingSource
:
ANSWER
Answered 2020-Aug-13 at 10:10It turns out that you need to increase the Room version to 2.3.0-alpha02
or above:
QUESTION
Suppose I have a API resource that displays a list of animals, let's call it /animals
, and I have another that is used to search the list of animals using their species, /animals/{species}
, if I were to use Android's new Paging3 library, would I have to create two separate PagingSource
objects for them? The Codelab for paging has one class that extends PagingSource
called GithubPagingSource
, but it is only for one endpoint. I was wondering what the correct approach would be.
ANSWER
Answered 2020-Aug-06 at 04:30A single instance of PagingSource is supposed to represent a snapshot of the backing dataset, which basically means that if a page is dropped and reloaded by the same instance, the same page should be output.
However, you can have one implementation and create multiple instances of it, so something like this would probably be recommendable:
QUESTION
Im trying to implement Paging but every time i rotate a screen constructor of view model gets called, thus triggering loadInitial to fetch new data from network in my DataSource class.Help appreciated
...ANSWER
Answered 2020-Jun-06 at 04:49You should never be calling through to the factory's create()
method yourself.
Instead, you should be following the documentation and using ViewModelProvider:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install android-paging
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page