How to clear/remove all items in page list adapter

2019-08-20 09:14发布

问题:

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)
    }

回答1:

submitting null clear the currently loaded page list

  productSearchResultItemAdapter.submitList(null)


回答2:

Before invalidate, clear your list data item. Like we did in simple way:

list.clear();
adapter.notifyDataSetChanged();