refactor: 优化货架列表转换逻辑并支持奇数长度处理
- 移除 subList 创建,直接通过索引访问元素 - 修复奇数长度列表的处理逻辑 - 保持单次循环,提升性能 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -231,11 +231,15 @@ class HomeActivity : BaseActivity() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
list.clear()
|
list.clear()
|
||||||
val subList01 = tempList.subList(0, tempList.size / 2)
|
val leftSize = (tempList.size + 1) / 2 // 左列长度(向上取整)
|
||||||
val subList02 = tempList.subList(tempList.size / 2, tempList.size)
|
val rightSize = tempList.size / 2 // 右列长度(向下取整)
|
||||||
repeat(tempList.size / 2) { index ->
|
repeat(rightSize) { index ->
|
||||||
list.add(subList01[index].also { it.deviceId = App.deviceId })
|
list.add(tempList[index].also { it.deviceId = App.deviceId })
|
||||||
list.add(subList02[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()
|
shelfAdapter.notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user