How to clear/remove all items in page list adapter

耗尽温柔 提交于 2019-12-11 03:18:53

问题


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:


In Java:

I cleared all items on in PagedListAdapter by calling invalidate() on DataSource instance like that

public void clear(){
   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




回答3:


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

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


来源:https://stackoverflow.com/questions/54856941/how-to-clear-remove-all-items-in-page-list-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!