9

我正在使用 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)
    }
4

5 回答 5

19

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

于 2020-08-21T05:37:06.037 回答
9

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

  productSearchResultItemAdapter.submitList(null)
于 2019-02-26T12:41:20.153 回答
4

在 Java 中:

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

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

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

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

然后加载您想要的任何数据

你可以在我的项目中看到我是如何做到的。

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

于 2019-12-01T18:53:11.947 回答
0

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>>? = 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
}

In videoRepository

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
}
于 2021-05-02T00:26:43.533 回答
-3

在无效之前,清除您的列表数据项。就像我们以简单的方式做的那样:

list.clear();
adapter.notifyDataSetChanged();
于 2019-02-25T05:00:30.827 回答