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

11

我正在使用Android分页库来显示搜索结果项,有没有办法清除/删除所有加载的结果项,调用Live Paged List上的Invalidate可以刷新列表但不会清除/删除项。

我正在使用Android分页库来显示搜索结果项,是否有方法可以清除/删除所有已加载的结果项?我尝试在Live Paged List上调用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)
    }

你找到任何可行的解决方案了吗? - Kashish Sharma
5个回答

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

9
提交空值会清除当前加载的页面列表。
  productSearchResultItemAdapter.submitList(null)

这就是我一直在寻找的解决方案 :) - Fatih
我一直收到这个错误:java.lang.IndexOutOfBoundsException: 检测到不一致。无效的项目位置。我该怎么办? - CanCoder
它并不总是有效,但仍然可以在添加新项目的同时保留旧项目。 - user924

4

Java中:

我通过在DataSource实例上调用invalidate()方法来清除PagedListAdapter中的所有项目。

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

在您的ViewModel中添加此方法,然后在活动中调用它。
movieViewModel.clear();
movieAdapter.notifyDataSetChanged();

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

您可以在我的项目中看到我如何做到这一点。

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


1
在片段中。
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
}

Sunny仅用于一次搜索。下一次搜索时,搜索值不会更改。 - Raj Kanchan

-3

在失效(invalidate)之前,清除列表数据项。

就像我们以简单的方式所做的一样:

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

你能提供一个清除pageList数据的例子吗? - Sam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接