I'm trying to implement a fragment which have an EditText and a RecyclerView. RecyclerView is filled with data coming through PagingDataAdapter
class SearchAdapter(
private val showFullPeriod: Boolean,
private val onItemClick: (Vacancy) -> Unit
) :
PagingDataAdapter<Vacancy, SearchViewHolder>(VacancyComparator()) {
in my fragment I define adapter like this
private val seafarerAdapter = SearchAdapter(false) { vacancy -> onItemClick(vacancy) }
then in my onViewCreated of the fragment I apply adapter to recyclerview
recyclerSeafarers.adapter = seafarerAdapter
in my viewModel i collect data like this
val vacanciesData: Flow<PagingData<Vacancy>> = newPagerInstance()
private fun newPagerInstance(): Flow<PagingData<Vacancy>> = Pager(
config = PagingConfig(pageSize = vacanciesUseCase.perPage, enablePlaceholders = false),
initialKey = 1,
pagingSourceFactory = { BasePagingSource(vacanciesUseCase) }
).flow.cachedIn(viewModelScope)
vacanciesUseCase is a class which is responsible for making queries to network and provide responses.
Everything works fine, until I'm not trying to change like this
vacanciesUseCase.text = text
after that i sent and event to my fragment and make adapter.refresh()
I works fine. The new data comes. The problems begin when bewore changing search text I`m scrolling down so adapter loads pages 2,3,4 etc. If I'm on the page 4. And change searching query and then call adapter.refresh() it still thinks I'm on the 4th page. It doesn't go to page 1. I can see it in logs of queries to network.
I tried recyclerView.scrollToPosition(0) before refresh but it doesn't work at all. I tried recyclerView.smoothhScrollToPosition(0). It works but not always and it scrolls really slow. I tried to make a delay between call recyclerView.scrollToPosition(0) and adapter.refresh(). It helps but it's a bad way, I think.
The only thing that helped is to make adapter a var and recreate it on textchange So my main question is how to make PagingAdapter completely refresh and go to page 1?