refactor(home): 用FlexboxLayoutManager替换GridLayoutManager,支持动态行列布局
FlexboxLayoutManager(COLUMN+WRAP)天然列优先排列,数据无需重排。 item宽高在拿到接口行列数后直接计算,适配任意rows×cols配置。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -87,6 +87,8 @@ dependencies {
|
||||
|
||||
implementation("org.greenrobot:eventbus:3.3.1")
|
||||
|
||||
implementation("com.google.android.flexbox:flexbox:3.0.0")
|
||||
|
||||
// implementation("io.github.jeremyliao:live-event-bus-x:1.8.0")
|
||||
// implementation("com.hoho:android-usb-serial:1.3.0")
|
||||
// implementation("io.github.jeadyx:jserialport:1.5")
|
||||
|
||||
@@ -35,8 +35,11 @@ import com.shuwei.intelligent.shelves.serial.ProtocolConstants
|
||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.C_TEMP_CMD
|
||||
import com.shuwei.intelligent.shelves.serial.ProtocolConstants.START_TEMP_CTRL_CMD
|
||||
import com.shuwei.intelligent.shelves.serial.ScaleManager
|
||||
import com.shuwei.intelligent.shelves.utils.GridLayoutTool
|
||||
import com.google.android.flexbox.FlexDirection
|
||||
import com.google.android.flexbox.FlexWrap
|
||||
import com.google.android.flexbox.FlexboxLayoutManager
|
||||
import com.shuwei.intelligent.shelves.utils.IntervalExecutor
|
||||
import com.shuwei.intelligent.shelves.utils.ext.dp
|
||||
import com.shuwei.intelligent.shelves.utils.ext.gone
|
||||
import com.shuwei.intelligent.shelves.utils.ext.toJsonString
|
||||
import com.shuwei.intelligent.shelves.utils.ext.toast
|
||||
@@ -199,10 +202,14 @@ class HomeV2Activity : BaseActivity() {
|
||||
return
|
||||
}
|
||||
list.clear()
|
||||
tempList = GridLayoutTool.initListSortV2(rows = rows, columns = columns, source = tempList)
|
||||
list.addAll(tempList)
|
||||
|
||||
val displayMetrics = resources.displayMetrics
|
||||
shelfAdapter.itemWidth = (displayMetrics.widthPixels - 24.dp) / columns - 24.dp
|
||||
shelfAdapter.itemHeight = (displayMetrics.heightPixels - 68.dp) / rows - 24.dp
|
||||
|
||||
if (binding.rvShelf.adapter == null) {
|
||||
initRecyclerView(columns)
|
||||
initRecyclerView()
|
||||
} else {
|
||||
shelfAdapter.notifyDataSetChanged()
|
||||
}
|
||||
@@ -328,8 +335,11 @@ class HomeV2Activity : BaseActivity() {
|
||||
// syncShelfGoodsToServer()
|
||||
}
|
||||
|
||||
private fun initRecyclerView(columns: Int) {
|
||||
val layoutManager = GridLayoutTool.getLayoutManagerV2(this, columns = columns)
|
||||
private fun initRecyclerView() {
|
||||
val layoutManager = FlexboxLayoutManager(this).apply {
|
||||
flexDirection = FlexDirection.COLUMN
|
||||
flexWrap = FlexWrap.WRAP
|
||||
}
|
||||
binding.rvShelf.let {
|
||||
it.layoutManager = layoutManager
|
||||
it.adapter = shelfAdapter
|
||||
@@ -443,7 +453,7 @@ class HomeV2Activity : BaseActivity() {
|
||||
if (cabinetInitResult == null) {
|
||||
return
|
||||
}
|
||||
val submitList = GridLayoutTool.submitListSortV2(list)
|
||||
val submitList = list.sortedBy { it.slotNo?.toIntOrNull() ?: 0 }
|
||||
viewModelV2.syncCabinetData(SyncBody().apply {
|
||||
// cabinetId = list[0].cabinetId
|
||||
cabinetId = App.deviceId
|
||||
|
||||
@@ -7,6 +7,7 @@ import androidx.annotation.ColorRes
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.chad.library.adapter4.BaseQuickAdapter
|
||||
import com.chad.library.adapter4.viewholder.QuickViewHolder
|
||||
import com.google.android.flexbox.FlexboxLayoutManager
|
||||
import com.shuwei.intelligent.shelves.R
|
||||
import com.shuwei.intelligent.shelves.databinding.ListItemShelfBinding
|
||||
import com.shuwei.intelligent.shelves.model.ShelfModelV2
|
||||
@@ -17,6 +18,12 @@ import kotlin.math.abs
|
||||
class ShelfV2Adapter(list: MutableList<ShelfModelV2>) :
|
||||
BaseQuickAdapter<ShelfModelV2, ShelfV2Adapter.VH>(list) {
|
||||
|
||||
/** 每个格口 item 的高度(像素),由 Activity 动态计算 */
|
||||
var itemHeight: Int = 0
|
||||
|
||||
/** 每个格口 item 的宽度(像素),由 Activity 动态计算 */
|
||||
var itemWidth: Int = 0
|
||||
|
||||
inner class VH(val binding: ListItemShelfBinding) : QuickViewHolder(binding.root)
|
||||
|
||||
override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
|
||||
@@ -26,6 +33,12 @@ class ShelfV2Adapter(list: MutableList<ShelfModelV2>) :
|
||||
|
||||
override fun onBindViewHolder(holder: VH, position: Int, item: ShelfModelV2?) {
|
||||
item ?: return
|
||||
// 动态设置 item 宽高
|
||||
val lp = holder.itemView.layoutParams as? FlexboxLayoutManager.LayoutParams
|
||||
lp?.apply {
|
||||
if (itemWidth > 0) width = itemWidth
|
||||
if (itemHeight > 0) height = itemHeight
|
||||
}
|
||||
val binding = holder.binding
|
||||
|
||||
// 格口编号
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/layoutCard"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="216dp"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_margin="12dp"
|
||||
app:cardBackgroundColor="@color/bg_card_blue"
|
||||
app:cardCornerRadius="12dp"
|
||||
|
||||
Reference in New Issue
Block a user