【问题标题】:How to clear/remove all items in page list adapter如何清除/删除页面列表适配器中的所有项目
【发布时间】:2019-07-18 07:26:47
【问题描述】:

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

【问题讨论】:

  • 您找到任何可行的解决方案了吗?

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


【解决方案1】:

如果您使用 PagingDataAdapter,searchAdapter.submitData(lifecycle, PagingData.empty()) 可以工作

【讨论】:

    【解决方案2】:

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

      productSearchResultItemAdapter.submitList(null)
    

    【讨论】:

    • 这是我一直在寻找的解决方案 :)
    • 我不断收到此错误:java.lang.IndexOutOfBoundsException:检测到不一致。无效的项目位置。我能做些什么呢?
    • 它并不总是有效,它仍然可以在添加新闻时保留旧项目
    【解决方案3】:

    在 Java 中:

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

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

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

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

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

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

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

    【讨论】:

      【解决方案4】:

      在片段中

      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
      }
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

        • 能否举例说明如何清除pageList中的数据
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-02
        • 1970-01-01
        • 2014-09-15
        • 1970-01-01
        • 2020-07-27
        • 1970-01-01
        相关资源
        最近更新 更多