refactor: 优化货架列表转换逻辑并支持奇数长度处理

- 移除 subList 创建,直接通过索引访问元素
- 修复奇数长度列表的处理逻辑
- 保持单次循环,提升性能

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-01 17:53:00 +08:00
co-authored by Claude Sonnet 4.6
parent 133f309d4b
commit 3d67dd2c2d
@@ -231,11 +231,15 @@ class HomeActivity : BaseActivity() {
return
}
list.clear()
val subList01 = tempList.subList(0, tempList.size / 2)
val subList02 = tempList.subList(tempList.size / 2, tempList.size)
repeat(tempList.size / 2) { index ->
list.add(subList01[index].also { it.deviceId = App.deviceId })
list.add(subList02[index].also { it.deviceId = App.deviceId })
val leftSize = (tempList.size + 1) / 2 // 左列长度(向上取整)
val rightSize = tempList.size / 2 // 右列长度(向下取整)
repeat(rightSize) { index ->
list.add(tempList[index].also { it.deviceId = App.deviceId })
list.add(tempList[leftSize + index].also { it.deviceId = App.deviceId })
}
// 处理奇数长度:左列比右列多一个
if (tempList.size % 2 != 0) {
list.add(tempList[rightSize].also { it.deviceId = App.deviceId })
}
shelfAdapter.notifyDataSetChanged()
}