How To Clear/remove All Items In Page List Adapter
I'm using the android paging library to show search result items, is there any way I can clear/remove all the loaded result items, Calling Invalidate on live Paged List refreshing
Solution 1:
If you're working with PagingDataAdapter, searchAdapter.submitData(lifecycle, PagingData.empty())
works
Solution 2:
submitting null clear the currently loaded page list
productSearchResultItemAdapter.submitList(null)
Solution 3:
In Java:
I cleared all items on in PagedListAdapter by calling invalidate() on DataSource instance like that
publicvoidclear(){
movieDataSource.invalidate();
}
Add this method in your ViewModel then call it in your activity
movieViewModel.clear();
movieAdapter.notifyDataSetChanged();
Then Load any data you want
You can see how I made it in my project.
Here is the Link: https://github.com/Marwa-Eltayeb/MovieTrailer
Solution 4:
In Fragment
lifecycleScope.launch {
viewModel.currentResult = null
viewModel.getSearchAudio(binding.etxtSearch.text.toString().trim(), 0).collectLatest { it ->
Log.v(mTAG, "Status: New record")
adapterAudioList.submitData(it)
}
}
In ViewModel
var currentResult: Flow<PagingData<AudioModel>>? = nullfungetSearchAudio(trackName: String, lastPageCount: Int): Flow<PagingData<AudioModel>> {
val lastResult = currentResult
if (lastResult != null) {
return lastResult
}
val newResult: Flow<PagingData<AudioModel>> = videoRepository.getAudioSearchPaging(trackName, lastPageCount).cachedIn(viewModelScope)
currentResult = newResult
return newResult
}
In videoRepository
fungetAudioSearchPaging(trackName: String, lastPageCount: Int): Flow<PagingData<AudioModel>> {
return Pager(
config = PagingConfig(pageSize = KeyConstants.AUDIO_PAGE_SIZE, enablePlaceholders = false),
pagingSourceFactory = { AudioSearchPagingSource(context, trackName, lastPageCount) },
).flow
}
Solution 5:
Before invalidate, clear your list data item. Like we did in simple way:
list.clear();
adapter.notifyDataSetChanged();
Post a Comment for "How To Clear/remove All Items In Page List Adapter"