修复了排序问题
This commit is contained in:
@@ -15,12 +15,12 @@ object GlobalData {
|
|||||||
/**
|
/**
|
||||||
* 横排数量
|
* 横排数量
|
||||||
*/
|
*/
|
||||||
var arrayCross: Int = 2
|
var arrayCross: Int = 5
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 竖排数量
|
* 竖排数量
|
||||||
*/
|
*/
|
||||||
var arrayVertical: Int = 10
|
var arrayVertical: Int = 11
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 排列方式 0 垂直 1 水平
|
* 排列方式 0 垂直 1 水平
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ import com.sw.platecabinet.databinding.ItemBindViewBinding
|
|||||||
import com.sw.platecabinet.ext.formatNumber
|
import com.sw.platecabinet.ext.formatNumber
|
||||||
import com.sw.platecabinet.ext.maskName
|
import com.sw.platecabinet.ext.maskName
|
||||||
import com.sw.platecabinet.model.response.EquipmentUserInfo
|
import com.sw.platecabinet.model.response.EquipmentUserInfo
|
||||||
|
import com.sw.platecabinet.utils.ListArrangementUtil
|
||||||
import kotlinx.coroutines.flow.drop
|
import kotlinx.coroutines.flow.drop
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import kotlin.math.ceil
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 餐盘柜绑定的用户列表界面
|
* 餐盘柜绑定的用户列表界面
|
||||||
@@ -43,7 +43,6 @@ class SettingListFragment :
|
|||||||
* 垂直滚动的adapter
|
* 垂直滚动的adapter
|
||||||
*/
|
*/
|
||||||
private var itemAdapter: GenericItemAdapter<EquipmentUserInfo, ItemBindViewBinding>? = null
|
private var itemAdapter: GenericItemAdapter<EquipmentUserInfo, ItemBindViewBinding>? = null
|
||||||
private val itemsPerPage = 2 * GlobalData.arrayVertical // 每行2个,每列11个,共22个
|
|
||||||
|
|
||||||
override fun registerDataChange() {
|
override fun registerDataChange() {
|
||||||
super.registerDataChange()
|
super.registerDataChange()
|
||||||
@@ -179,63 +178,27 @@ 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>) {
|
||||||
|
val itemsPerPage = 2 * GlobalData.arrayVertical // 每行2个,每列11个,共22个
|
||||||
if (GlobalData.arrayCross > 2) {
|
if (GlobalData.arrayCross > 2) {
|
||||||
|
// 多recyclerview
|
||||||
var pages = if (items.size > itemsPerPage) {
|
var pages = if (items.size > itemsPerPage) {
|
||||||
if (GlobalData.arrayMode == 1) {
|
if (GlobalData.arrayMode == 0) {
|
||||||
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()
|
||||||
if (index == pageList.size - 1) {
|
if (index == pageList.size - 1) {
|
||||||
itemList = (itemList + List(GlobalData.arrayVertical) {
|
// 补全剩余item
|
||||||
|
itemList = (itemList + List(itemsPerPage - itemList.size) {
|
||||||
EquipmentUserInfo()
|
EquipmentUserInfo()
|
||||||
}) as MutableList<EquipmentUserInfo>
|
}) as MutableList<EquipmentUserInfo>
|
||||||
}
|
}
|
||||||
convertToColumnFirst(itemList, GlobalData.arrayVertical)
|
ListArrangementUtil.convertToColumnFirst(itemList, GlobalData.arrayVertical)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
val newItems =
|
||||||
|
ListArrangementUtil.horizontalSortPageItem(items, GlobalData.arrayCross)
|
||||||
|
newItems.chunked(itemsPerPage)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
listOf(
|
listOf(
|
||||||
@@ -244,28 +207,15 @@ class SettingListFragment :
|
|||||||
}
|
}
|
||||||
pageAdapter?.updatePages(pages)
|
pageAdapter?.updatePages(pages)
|
||||||
} else {
|
} else {
|
||||||
|
// 单 recyclerview
|
||||||
var pages = items
|
var pages = items
|
||||||
if (GlobalData.arrayMode == 0) {
|
if (GlobalData.arrayMode == 0) {
|
||||||
pages = interleaveEquipmentUsers(pages)
|
pages = ListArrangementUtil.verticalSortItem(pages)
|
||||||
}
|
}
|
||||||
itemAdapter?.updateData(pages)
|
itemAdapter?.updateData(pages)
|
||||||
}
|
}
|
||||||
binding.mainRecyclerView.scrollToPosition(0)
|
binding.mainRecyclerView.scrollToPosition(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 重新排列数组
|
|
||||||
*/
|
|
||||||
private fun convertToColumnFirst(
|
|
||||||
original: List<EquipmentUserInfo>,
|
|
||||||
cols: Int
|
|
||||||
): List<EquipmentUserInfo> {
|
|
||||||
val rows = ceil(original.size.toDouble() / cols).toInt()
|
|
||||||
return List(original.size) { pos ->
|
|
||||||
val row = pos % rows
|
|
||||||
val col = pos / rows
|
|
||||||
val originalPos = col + row * cols
|
|
||||||
original[originalPos]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package com.sw.inbound.utils
|
|||||||
|
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import timber.log.Timber
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
@@ -67,7 +68,8 @@ object DateTimeUtils {
|
|||||||
val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
|
val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
|
||||||
format.parse(timeString)
|
format.parse(timeString)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
// e.printStackTrace()
|
||||||
|
Timber.e(e.message)
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
package com.sw.platecabinet.utils
|
||||||
|
|
||||||
|
import com.sw.platecabinet.model.response.EquipmentUserInfo
|
||||||
|
import timber.log.Timber
|
||||||
|
import kotlin.math.ceil
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 列表排序工具类
|
||||||
|
*/
|
||||||
|
object ListArrangementUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* page item水平排序
|
||||||
|
*/
|
||||||
|
fun horizontalSortPageItem(
|
||||||
|
list: List<EquipmentUserInfo>,
|
||||||
|
columns: Int,
|
||||||
|
defaultValue: EquipmentUserInfo = EquipmentUserInfo()
|
||||||
|
): List<EquipmentUserInfo> {
|
||||||
|
Timber.d("horizontalSortPageItem")
|
||||||
|
// 填充缺失的数据,确保能被 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* item 垂直排序
|
||||||
|
*/
|
||||||
|
fun verticalSortItem(list: List<EquipmentUserInfo>): List<EquipmentUserInfo> {
|
||||||
|
Timber.d("verticalSortItem")
|
||||||
|
val users = if (list.size % 2 != 0) {
|
||||||
|
Timber.d("列表长度必须是偶数")
|
||||||
|
list + EquipmentUserInfo()
|
||||||
|
} else {
|
||||||
|
list
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* page item垂直排序
|
||||||
|
*/
|
||||||
|
fun convertToColumnFirst(
|
||||||
|
original: List<EquipmentUserInfo>,
|
||||||
|
cols: Int
|
||||||
|
): List<EquipmentUserInfo> {
|
||||||
|
Timber.d("convertToColumnFirst")
|
||||||
|
val rows = ceil(original.size.toDouble() / cols).toInt()
|
||||||
|
return List(original.size) { pos ->
|
||||||
|
val row = pos % rows
|
||||||
|
val col = pos / rows
|
||||||
|
val originalPos = col + row * cols
|
||||||
|
original[originalPos]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user