初步实现功能
This commit is contained in:
@@ -15,7 +15,7 @@ object GlobalData {
|
|||||||
/**
|
/**
|
||||||
* 横排数量
|
* 横排数量
|
||||||
*/
|
*/
|
||||||
var arrayCross: Int = 3
|
var arrayCross: Int = 2
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 竖排数量
|
* 竖排数量
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ class SettingListFragment :
|
|||||||
item: EquipmentUserInfo,
|
item: EquipmentUserInfo,
|
||||||
position: Int
|
position: Int
|
||||||
) {
|
) {
|
||||||
if (item.equipmentBoxCode?.isEmpty() == true) {
|
if (item.isPlaceholder() == true) {
|
||||||
this.llRoot.isVisible = false
|
this.llRoot.isVisible = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -179,9 +179,53 @@ class SettingListFragment :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun reorderListAutoFill(
|
||||||
|
list: List<EquipmentUserInfo>,
|
||||||
|
columns: Int,
|
||||||
|
defaultValue: EquipmentUserInfo = EquipmentUserInfo()
|
||||||
|
): List<EquipmentUserInfo> {
|
||||||
|
// 填充缺失的数据,确保能被 columns 整除
|
||||||
|
val filledList = if (list.size % columns != 0) {
|
||||||
|
list + List(columns - (list.size % columns)) { defaultValue }
|
||||||
|
} else {
|
||||||
|
list
|
||||||
|
}
|
||||||
|
|
||||||
|
val chunks = filledList.chunked(columns)
|
||||||
|
return (0 until columns)
|
||||||
|
.windowed(2, 2, partialWindows = true)
|
||||||
|
.flatMap { group ->
|
||||||
|
when (group.size) {
|
||||||
|
2 -> chunks.flatMap { listOf(it[group[0]], it[group[1]]) }
|
||||||
|
1 -> chunks.flatMap {
|
||||||
|
listOf(
|
||||||
|
it[group[0]],
|
||||||
|
EquipmentUserInfo(equipmentBoxCode = null)
|
||||||
|
)
|
||||||
|
}.dropLast(1)
|
||||||
|
|
||||||
|
else -> error("Unexpected group size")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun interleaveEquipmentUsers(users: List<EquipmentUserInfo>): List<EquipmentUserInfo> {
|
||||||
|
require(users.size % 2 == 0) { "列表长度必须是偶数" }
|
||||||
|
|
||||||
|
val half = users.size / 2
|
||||||
|
val firstHalf = users.take(half)
|
||||||
|
val secondHalf = users.drop(half)
|
||||||
|
|
||||||
|
return firstHalf.zip(secondHalf) { a, b -> listOf(a, b) }.flatten()
|
||||||
|
}
|
||||||
|
|
||||||
fun updateAdapter(items: List<EquipmentUserInfo>) {
|
fun updateAdapter(items: List<EquipmentUserInfo>) {
|
||||||
if (GlobalData.arrayCross > 2) {
|
if (GlobalData.arrayCross > 2) {
|
||||||
var pages = if (items.size > itemsPerPage) {
|
var pages = if (items.size > itemsPerPage) {
|
||||||
|
if (GlobalData.arrayMode == 1) {
|
||||||
|
val newItems = reorderListAutoFill(items, GlobalData.arrayCross)
|
||||||
|
newItems.chunked(itemsPerPage)
|
||||||
|
} else {
|
||||||
val pageList = items.chunked(itemsPerPage)
|
val pageList = items.chunked(itemsPerPage)
|
||||||
pageList.mapIndexed { index, it ->
|
pageList.mapIndexed { index, it ->
|
||||||
var itemList: MutableList<EquipmentUserInfo> = it.toMutableList()
|
var itemList: MutableList<EquipmentUserInfo> = it.toMutableList()
|
||||||
@@ -192,16 +236,19 @@ class SettingListFragment :
|
|||||||
}
|
}
|
||||||
convertToColumnFirst(itemList, GlobalData.arrayVertical)
|
convertToColumnFirst(itemList, GlobalData.arrayVertical)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
listOf(
|
listOf(
|
||||||
// 根据条件判断是否需要重新排列
|
|
||||||
items
|
items
|
||||||
// convertToColumnFirst(items, GlobalData.arrayVertical)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
pageAdapter?.updatePages(pages)
|
pageAdapter?.updatePages(pages)
|
||||||
} else {
|
} else {
|
||||||
itemAdapter?.updateData(items)
|
var pages = items
|
||||||
|
if (GlobalData.arrayMode == 0) {
|
||||||
|
pages = interleaveEquipmentUsers(pages)
|
||||||
|
}
|
||||||
|
itemAdapter?.updateData(pages)
|
||||||
}
|
}
|
||||||
binding.mainRecyclerView.scrollToPosition(0)
|
binding.mainRecyclerView.scrollToPosition(0)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,4 +73,8 @@ data class EquipmentUserInfo(
|
|||||||
fun isOtherEquipment(): Boolean {
|
fun isOtherEquipment(): Boolean {
|
||||||
return GlobalData.globalEquipmentCode != equipmentCode
|
return GlobalData.globalEquipmentCode != equipmentCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isPlaceholder(): Boolean {
|
||||||
|
return equipmentBoxCode == null || equipmentBoxCode == ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user