Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the list not clear/remove items

In Activity: 
 fun clearSearchResult() {
        if (productSearchResultItemAdapter.itemCount > 0) {
            viewModel.invalidateResultList()
        }
    }


In ViewModel
 private fun searchProductByTerm(searchTerm: String): Listing<Item> {
        sourceFactory = ProductSearchItemDataSourceFactory(productSearchUC, compositeDisposable, searchTerm, resourceResolver)

        val livePagedList = LivePagedListBuilder(sourceFactory, pagedListConfig)
                //The executor used to fetch additional pages from the DataSource
                .setFetchExecutor(getNetworkExecutor())
                .build()

        return Listing(
                pagedList = livePagedList,
                networkState = switchMap(sourceFactory.sourceLiveData) {
                    it.networkState
                },
                retry = {
                    sourceFactory.sourceLiveData.value?.retryAllFailed()
                }
        )

    }


    fun invalidateResultList() {
        sourceFactory?.sourceLiveData?.value?.invalidate()
    }

private val productSearchName = MutableLiveData<String>()
    private val repoResult = map(productSearchName) {
        searchProductByTerm(it)
    }
like image 803
Sam Avatar asked Feb 24 '19 21:02

Sam


1 Answers

If you're working with PagingDataAdapter, searchAdapter.submitData(lifecycle, PagingData.empty()) works

like image 88
Ado Ki Avatar answered Oct 13 '22 16:10

Ado Ki