som*_*234 8 android kotlin android-recyclerview android-paging
经过大量搜索之后,我知道使用常规适配器是可行的,但是我不知道如何使用分页库来实现。我不需要代码只是一个提示。
例

Vah*_*yan 12
insertSeparators您可以使用Paging 3库获得相同的结果。确保您的物品sorted按日期列出。
里面或viewmodel检索Pager类似的东西
private val communicationResult: Flow<PagingData<CommunicationHistoryItem>> = Pager(
PagingConfig(
pageSize = 50,
enablePlaceholders = false,
maxSize = 400,
initialLoadSize = 50
)
) {
CommunicationPagingSource(repository)
}.flow.cachedIn(viewModelScope)
Run Code Online (Sandbox Code Playgroud)
毕竟insert separators就像一个标题
val groupedCommunicationResult = communicationResult
.map { pagingData -> pagingData.map { CommunicationHistoryModel.Body(it) } }
.map {
it.insertSeparators{ after, before ->
if (before == null) {
//the end of the list
return@insertSeparators null
}
val afterDateStr = after?.createdDate
val beforeDateStr = before.createdDate
if (afterDateStr == null || beforeDateStr == null)
return@insertSeparators null
val afterDate = DateUtil.parseAsCalendar(afterDateStr)?.cleanTime()?.time ?: 0
val beforeDate = DateUtil.parseAsCalendar(beforeDateStr)?.cleanTime()?.time ?: 0
if (afterDate > beforeDate) {
CommunicationHistoryModel.Header( DateUtil.format(Date(beforeDate))) // dd.MM.yyyy
} else {
// no separator
null
}
}
}
Run Code Online (Sandbox Code Playgroud)
cleanTime需要grouping忽略dd.MM.yyyy时间
fun Calendar.cleanTime(): Date {
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
return this.time
}
Run Code Online (Sandbox Code Playgroud)
要添加分隔符,您实际上有两个选择:
对于分页库,仅选项2是可行的,因为它仅部分加载数据,并且插入分隔符变得更加复杂。您只需要想出一种方法来检查项目x是否与项目不同,x-1并根据结果在视图中显示/隐藏日期部分。
我和你在同一个地方,我想出了这个解决方案。
一个重要的注意事项,为了实现这一点,我不得不将我的日期转换器更改为数据库,从 long 到 string 以存储时间戳
这些是我的转换器
class DateConverter {
companion object {
@JvmStatic
val formatter = SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH)
@TypeConverter
@JvmStatic
fun toDate(text: String): Date = formatter.parse(text)
@TypeConverter
@JvmStatic
fun toText(date: Date): String = formatter.format(date)
}
}
Run Code Online (Sandbox Code Playgroud)
虽然有一些起始信息,但我有一个我希望显示的报告标题列表,可以翻页并能够过滤
它们由这个对象表示:
data class ReportHeaderEntity(
@ColumnInfo(name = "id") override val id: UUID
, @ColumnInfo(name = "name") override val name: String
, @ColumnInfo(name = "description") override val description: String
, @ColumnInfo(name = "created") override val date: Date)
Run Code Online (Sandbox Code Playgroud)
我还想在列表中的项目之间添加分隔符以按日期显示它们
我通过执行以下操作实现了这一点:
我在这样的房间中创建了一个新查询
@Query(
"SELECT id, name, description,created " +
"FROM (SELECT id, name, description, created, created AS sort " +
" FROM reports " +
" WHERE :filter = '' " +
" OR name LIKE '%' || :filter || '%' " +
" OR description LIKE '%' || :filter || '%' " +
" UNION " +
" SELECT '00000000-0000-0000-0000-000000000000' as id, Substr(created, 0, 9) as name, '' as description, Substr(created, 0, 9) || '000000' AS created, Substr(created, 0, 9) || '256060' AS sort " +
" FROM reports " +
" WHERE :filter = '' " +
" OR name LIKE '%' || :filter || '%' " +
" OR description LIKE '%' || :filter || '%' " +
" GROUP BY Substr(created, 0, 9)) " +
"ORDER BY sort DESC ")
fun loadReportHeaders(filter: String = ""): DataSource.Factory<Int, ReportHeaderEntity>
Run Code Online (Sandbox Code Playgroud)
这基本上为我过滤的所有项目创建了一个分隔线
它还创建了一个用于排序的虚拟日期(时间为 25:60:60,以便它始终出现在其他报告的前面)
然后我使用 union 将它与我的列表结合起来,并按虚拟日期对它们进行排序
我必须从 long 更改为 string 的原因是因为在 sql 中使用 string 创建虚拟日期并将日期部分与整个日期时间分开要容易得多
上面创建了一个这样的列表:
00000000-0000-0000-0000-000000000000 20190522 20190522000000
e3b8fbe5-b8ce-4353-b85d-8a1160f51bac name 16769 description 93396 20190522141926
6779fbea-f840-4859-a9a1-b34b7e6520be name 86082 description 21138 20190522141925
00000000-0000-0000-0000-000000000000 20190521 20190521000000
6efa201f-d618-4819-bae1-5a0e907ddcfb name 9702 description 84139 20190521103247
Run Code Online (Sandbox Code Playgroud)
在我的 PagedListAdapter 中,我将其更改为PagedListAdapter<ReportHeader, RecyclerView.ViewHolder>(不是特定的查看者)的实现
添加到伴生对象:
companion object {
private val EMPTY_ID = UUID(0L,0L)
private const val LABEL = 0
private const val HEADER = 1
}
Run Code Online (Sandbox Code Playgroud)
并像这样覆盖 get 视图类型:
override fun getItemViewType(position: Int): Int = if (getItem(position)?.id ?: EMPTY_ID == EMPTY_ID) LABEL else HEADER
Run Code Online (Sandbox Code Playgroud)
然后我创建了两个单独的视图持有者:
class ReportHeaderViewHolder(val binding: ListItemReportBinding) : RecyclerView.ViewHolder(binding.root)
class ReportLabelViewHolder(val binding: ListItemReportLabelBinding) : RecyclerView.ViewHolder(binding.root)
Run Code Online (Sandbox Code Playgroud)
并实现了其他覆盖方法,如下所示:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
HEADER -> ReportHeaderViewHolder(DataBindingUtil.inflate(inflater, R.layout.list_item_report, parent, false))
else -> ReportLabelViewHolder(DataBindingUtil.inflate(inflater, R.layout.list_item_report_label, parent, false))
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val reportItem = getItem(position)
when (getItemViewType(position)) {
HEADER -> {
(holder as ReportHeaderViewHolder).binding.apply {
report = reportItem
executePendingBindings()
}
}
LABEL -> {
(holder as ReportLabelViewHolder).binding.apply {
date = reportItem?.name
executePendingBindings()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这有助于并激励人们找到更好的解决方案
| 归档时间: |
|
| 查看次数: |
1234 次 |
| 最近记录: |