android-paging-3

Android Paging 3 library: how to change list with new param?

元气小坏坏 提交于 2021-01-29 12:36:35
问题 I have a search fragment that shows list of searched items. if user type something, I pass that string to url as new query parameter and get new list using paging 3 library. first solution is: //viewModel lateinit var postListUrl: String val postList: Flow<PagingData<Post>> = Pager(PagingConfig(pageSize = 20)) { PostPagingSource(postRepository, postListUrl) }.flow.cachedIn(viewModelScope) //fragment fun showPostList(url: String) { postListAdapter.submitData(lifecycle, PagingData.empty())

What does the initialKey parameter do in the Pager constructor

﹥>﹥吖頭↗ 提交于 2021-01-29 12:22:09
问题 I am trying to understand what the initialKey does in the Pager constructor, but I cannot find any sort of explanation, not even in the official documentation: https://developer.android.com/reference/kotlin/androidx/paging/Pager#%3Cinit%3E(androidx.paging.PagingConfig,%20androidx.paging.Pager.Key,%20androidx.paging.RemoteMediator,%20kotlin.Function0) Can anyone explain what it does or how it works and how to use it? 回答1: After testing I believe the initialKey param is meant to indicate a

Android Paging 3 library: how to change list with new param?

こ雲淡風輕ζ 提交于 2021-01-29 10:49:29
问题 I have a search fragment that shows list of searched items. if user type something, I pass that string to url as new query parameter and get new list using paging 3 library. first solution is: //viewModel lateinit var postListUrl: String val postList: Flow<PagingData<Post>> = Pager(PagingConfig(pageSize = 20)) { PostPagingSource(postRepository, postListUrl) }.flow.cachedIn(viewModelScope) //fragment fun showPostList(url: String) { postListAdapter.submitData(lifecycle, PagingData.empty())

How To Test PagingData From Paging 3

痴心易碎 提交于 2021-01-29 09:11:11
问题 My ViewModel has a method which returns a flow of PagingData . In my app, the data is fetched from the remote server, which is then saved to Room (the single source of truth): fun getChocolates(): Flow<PagingData<Chocolate>> { val pagingSourceFactory = { dao().getChocolateListData() } return Pager( config = PagingConfig( pageSize = NETWORK_PAGE_SIZE, maxSize = MAX_MEMORY_SIZE, enablePlaceholders = false ), remoteMediator = ChocolateRemoteMediator( api, dao ), pagingSourceFactory =

onRestoreInstanceState not working for RecyclerView layout manager if using Paging 3 library

纵饮孤独 提交于 2021-01-28 09:11:51
问题 I had a problem in saving RecyclerView state and it solved by saving layout manager state and using it after resume fragment.(thanks to @HarisDautović) class TestFragment : Fragment() { private val testListAdapter: TestListAdapter by lazy { TestListAdapter() } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_test, container, false) } override fun onViewCreated(view: View,