使用 Adapter Design Patterns 和 Jetpack Paging 将 Clean Architecture 原则应用于 recyclerview paging
apply Clean Architecture principles to recycler view pagging with Adapter Design Patterns and Jetpack Pagging
我想按照干净的架构原则构建一个应用程序。有一个带分页的回收器视图,它询问域层中的数据,域从数据层获取
作为
List<ToiletPaper>
。我想使用 Jetpack 中的 Pagging 库,但它 returns
PagedList<ToiletPaper>
由于数据层和域层不应绑定到任何平台或框架,因此我不想将它们包含在这些层中。如何在不改变Domain层的情况下使用Adapter Design Pattern添加Jetpack Paging Library?
All data in a PagedList is loaded from its DataSource. Creating a
PagedList loads the first chunk of data from the DataSource
immediately, and should for this reason be done on a background
thread. The constructed PagedList may then be passed to and used on
the UI thread.
在 Clean Architecture 中,我们将数据源放在数据层中。这意味着如果您决定使用 Jetpack 的分页库,则需要将该库作为数据和域依赖项的一部分导入。
我将为您提供在一般情况下如何在 Clean Architecture 中实现它的分步指南:
数据层
- 创建 Room Database (recommended here) 或数据源工厂(困难的方法)。
创建一个方法来 query/get 来自数据源的数据,returns DataSource.Factory
.
@Dao
interface ToiletDao {
@Query("SELECT * FROM toilet")
fun getToiletPapers(): DataSource.Factory<Int, ToiletPaper>
}
在 Repository 中提供一个方法来访问您刚刚创建的方法。
fun getToiletPapers(): DataSource.Factory<Int, ToiletPaper> {
return toiletDao.getToiletPapers()
}
领域层
创建用例或交互器以从存储库访问 getToiletPapers()
,并将其转换为 LiveData<PagedList<>>
:
class GetToiletPapers @Inject constructor(
private val repository: ToiletRepository
) : UseCase() {
fun execute(): DataSource.Factory<Int, ToiletPaper> {
repository.getToiletPapers()
}
}
注意:这个class是一个例子为了更容易解释。
表示层
在您的 ViewModel class 中,调用 getToiletPapers.execute().toLiveData(pageSize = 10)
并将值存储在 LiveData
.
中
使用PagedListAdapter
加载数据到RecyclerView。
PagedListAdapter 的使用方法我就不多说了,社区里有这样的问题的答案。希望本指南对您有所帮助!
我想按照干净的架构原则构建一个应用程序。有一个带分页的回收器视图,它询问域层中的数据,域从数据层获取
作为
List<ToiletPaper>
。我想使用 Jetpack 中的 Pagging 库,但它 returns
PagedList<ToiletPaper>
由于数据层和域层不应绑定到任何平台或框架,因此我不想将它们包含在这些层中。如何在不改变Domain层的情况下使用Adapter Design Pattern添加Jetpack Paging Library?
All data in a PagedList is loaded from its DataSource. Creating a PagedList loads the first chunk of data from the DataSource immediately, and should for this reason be done on a background thread. The constructed PagedList may then be passed to and used on the UI thread.
在 Clean Architecture 中,我们将数据源放在数据层中。这意味着如果您决定使用 Jetpack 的分页库,则需要将该库作为数据和域依赖项的一部分导入。
我将为您提供在一般情况下如何在 Clean Architecture 中实现它的分步指南:
数据层
- 创建 Room Database (recommended here) 或数据源工厂(困难的方法)。
创建一个方法来 query/get 来自数据源的数据,returns
DataSource.Factory
.@Dao interface ToiletDao { @Query("SELECT * FROM toilet") fun getToiletPapers(): DataSource.Factory<Int, ToiletPaper> }
在 Repository 中提供一个方法来访问您刚刚创建的方法。
fun getToiletPapers(): DataSource.Factory<Int, ToiletPaper> { return toiletDao.getToiletPapers() }
领域层
创建用例或交互器以从存储库访问 getToiletPapers()
,并将其转换为 LiveData<PagedList<>>
:
class GetToiletPapers @Inject constructor(
private val repository: ToiletRepository
) : UseCase() {
fun execute(): DataSource.Factory<Int, ToiletPaper> {
repository.getToiletPapers()
}
}
注意:这个class是一个例子为了更容易解释。
表示层
在您的 ViewModel class 中,调用
getToiletPapers.execute().toLiveData(pageSize = 10)
并将值存储在LiveData
. 中
使用
PagedListAdapter
加载数据到RecyclerView。
PagedListAdapter 的使用方法我就不多说了,社区里有这样的问题的答案。希望本指南对您有所帮助!