Map LiveData<PagedList<X>> to LiveData<PagedList<Y>>

久未见 提交于 2020-05-10 07:12:35

问题


I need to map domain objects to UI objects and display using a live paged list.

I have tried to map LiveData<PagedList<X>> to LiveData<PagedList<Y>>, and map PositionalDataSource<X> to PositionalDataSource<Y>, but due to package private and private restrictions these both appear to be impossible without placing my code in android.arch.paging package and using reflection, or using a modified version of the paging lib.

Does anyone know of a way to do this without resorting to such undesirable methods?

(Note that this would not be an issue if the paging library API used interfaces instead of abstract base classes - that would allow wrapping any paged list/data source and add appropriate mappings.)


回答1:


DataSource and DataSource.Factory have mapBy() and mapPageBy(), which could help you in this case. Just be careful cause these two will restrict the size of the "Y"-result-list.

If the size of the result differs from the original list's size then DataSource will throw an Exception.




回答2:


For me following worked :

  val dataSourceFactory = 
         cache.getDataSourceFactory(params)
              .map {
                   convertXToY(it)
                  }



来源:https://stackoverflow.com/questions/48872269/map-livedatapagedlistx-to-livedatapagedlisty

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