如何清除/删除页面列表适配器中的所有项目

时间:2019-02-24 21:55:46

标签: android android-architecture-components android-paging-library

我正在使用android分页库显示搜索结果项,有什么方法可以清除/删除所有已加载的结果项,在活动的分页列表上调用Invalidate刷新列表,而不清除/删除项

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

5 个答案:

答案 0 :(得分:4)

如果您正在使用PagingDataAdapter,则searchAdapter.submitData(lifecycle, PagingData.empty())可以使用

答案 1 :(得分:0)

在失效之前,请清除列表数据项。 就像我们以简单的方式所做的一样:

SuperInterface.super.hashCode();

答案 2 :(得分:0)

提交null清除当前加载的页面列表

  productSearchResultItemAdapter.submitList(null)

答案 3 :(得分:0)

在Java中:

我通过像这样在DataSource实例上调用invalidate()来清除PagedListAdapter中的所有项目

public void clear(){
   movieDataSource.invalidate();
}

在您的ViewModel中添加此方法,然后在您的活动中调用

movieViewModel.clear();
movieAdapter.notifyDataSetChanged();

然后加载所需的任何数据

您可以看到我在项目中的制作方式。

以下是链接:https://github.com/Marwa-Eltayeb/MovieTrailer

答案 4 :(得分:0)

在片段中

lifecycleScope.launch {
                viewModel.currentResult = null
                viewModel.getSearchAudio(binding.etxtSearch.text.toString().trim(), 0).collectLatest { it ->
                    Log.v(mTAG, "Status: New record")
                    adapterAudioList.submitData(it)
                }
            }
               

在视图模型中

var currentResult: Flow<PagingData<AudioModel>>? = null
fun getSearchAudio(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
}

在视频库中

fun getAudioSearchPaging(trackName: String, lastPageCount: Int): Flow<PagingData<AudioModel>> {
    return Pager(
        config = PagingConfig(pageSize = KeyConstants.AUDIO_PAGE_SIZE, enablePlaceholders = false),
        pagingSourceFactory = { AudioSearchPagingSource(context, trackName, lastPageCount) },
    ).flow
}