diff --git a/module_base/src/main/java/com/lukouguoji/module_base/bean/GjcCheckInRecord.kt b/module_base/src/main/java/com/lukouguoji/module_base/bean/GjcCheckInRecord.kt index 488afec..b5be258 100644 --- a/module_base/src/main/java/com/lukouguoji/module_base/bean/GjcCheckInRecord.kt +++ b/module_base/src/main/java/com/lukouguoji/module_base/bean/GjcCheckInRecord.kt @@ -1,5 +1,9 @@ package com.lukouguoji.module_base.bean +import androidx.databinding.BaseObservable +import androidx.databinding.Bindable +import androidx.databinding.library.baseAdapters.BR + /** * 国际出港计重记录明细Bean * 用于:加载运单的所有计重记录 + 批量更新 @@ -18,18 +22,34 @@ data class GjcCheckInRecord( var weight: Double = 0.0, // 运抵重量 var volume: Double = 0.0, // 运抵体积 var whId: Long = 0 // GJC_WAREHOUSE.ID -) { +) : BaseObservable() { + + // 数据变化回调 + var onDataChanged: (() -> Unit)? = null + // 件数的字符串表示(用于双向绑定) + @get:Bindable var pcStr: String get() = if (pc == 0L) "" else pc.toString() set(value) { - pc = value.toLongOrNull() ?: 0L + val newPc = value.toLongOrNull() ?: 0L + if (pc != newPc) { + pc = newPc + notifyPropertyChanged(BR.pcStr) + onDataChanged?.invoke() + } } // 重量的字符串表示(用于双向绑定) + @get:Bindable var weightStr: String get() = if (weight == 0.0) "" else weight.toString() set(value) { - weight = value.toDoubleOrNull() ?: 0.0 + val newWeight = value.toDoubleOrNull() ?: 0.0 + if (weight != newWeight) { + weight = newWeight + notifyPropertyChanged(BR.weightStr) + onDataChanged?.invoke() + } } } diff --git a/module_gjc/src/main/java/com/lukouguoji/gjc/activity/GjcWeighingRecordDetailsActivity.kt b/module_gjc/src/main/java/com/lukouguoji/gjc/activity/GjcWeighingRecordDetailsActivity.kt index a81feb2..d12d875 100644 --- a/module_gjc/src/main/java/com/lukouguoji/gjc/activity/GjcWeighingRecordDetailsActivity.kt +++ b/module_gjc/src/main/java/com/lukouguoji/gjc/activity/GjcWeighingRecordDetailsActivity.kt @@ -3,6 +3,7 @@ package com.lukouguoji.gjc.activity import android.content.Context import android.content.Intent import android.os.Bundle +import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import com.alibaba.android.arouter.facade.annotation.Route import com.lukouguoji.gjc.R @@ -24,6 +25,9 @@ class GjcWeighingRecordDetailsActivity : private lateinit var adapter: CommonAdapter + // 当前编辑模式(供ViewHolder获取) + var currentEditMode = false + override fun layoutId() = R.layout.activity_gjc_weighing_record_details override fun viewModelClass() = GjcWeighingRecordDetailsViewModel::class.java @@ -34,7 +38,13 @@ class GjcWeighingRecordDetailsActivity : binding.viewModel = viewModel // 初始化RecyclerView - binding.recyclerView.layoutManager = LinearLayoutManager(this) + val layoutManager = LinearLayoutManager(this) + binding.recyclerView.layoutManager = layoutManager + + // 添加分割线 + val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL) + binding.recyclerView.addItemDecoration(divider) + adapter = CommonAdapter( this, R.layout.item_gjc_check_in_record, @@ -47,17 +57,13 @@ class GjcWeighingRecordDetailsActivity : adapter.refresh(records) } - // 监听编辑模式变化,更新所有ViewHolder的编辑状态 + // 监听编辑模式变化,更新当前编辑模式并刷新列表 viewModel.isEditMode.observe(this) { isEditMode -> - val layoutManager = binding.recyclerView.layoutManager as? LinearLayoutManager - layoutManager?.let { - for (i in 0 until adapter.itemCount) { - val holder = binding.recyclerView.findViewHolderForAdapterPosition(i) - as? GjcCheckInRecordViewHolder - holder?.updateEditMode(isEditMode) - adapter.notifyItemChanged(i) - } - } + // 更新当前编辑模式状态 + currentEditMode = isEditMode + + // 刷新所有可见的 ViewHolder + adapter.notifyDataSetChanged() } // 初始化数据 diff --git a/module_gjc/src/main/java/com/lukouguoji/gjc/holder/GjcCheckInRecordViewHolder.kt b/module_gjc/src/main/java/com/lukouguoji/gjc/holder/GjcCheckInRecordViewHolder.kt index 38c084c..1b2a843 100644 --- a/module_gjc/src/main/java/com/lukouguoji/gjc/holder/GjcCheckInRecordViewHolder.kt +++ b/module_gjc/src/main/java/com/lukouguoji/gjc/holder/GjcCheckInRecordViewHolder.kt @@ -1,6 +1,7 @@ package com.lukouguoji.gjc.holder import android.view.View +import com.lukouguoji.gjc.activity.GjcWeighingRecordDetailsActivity import com.lukouguoji.gjc.databinding.ItemGjcCheckInRecordBinding import com.lukouguoji.module_base.base.BaseViewHolder import com.lukouguoji.module_base.bean.GjcCheckInRecord @@ -11,14 +12,13 @@ import com.lukouguoji.module_base.bean.GjcCheckInRecord class GjcCheckInRecordViewHolder(view: View) : BaseViewHolder(view) { - private var isEditMode: Boolean = false - - fun updateEditMode(editMode: Boolean) { - this.isEditMode = editMode - } - override fun onBind(item: Any?, position: Int) { val record = getItemBean(item)!! + + // 从Activity获取当前编辑模式 + val activity = itemView.context as? GjcWeighingRecordDetailsActivity + val isEditMode = activity?.currentEditMode ?: false + binding.record = record binding.isEditMode = isEditMode binding.position = position // 传入位置用于显示序号 diff --git a/module_gjc/src/main/java/com/lukouguoji/gjc/viewModel/GjcWeighingRecordDetailsViewModel.kt b/module_gjc/src/main/java/com/lukouguoji/gjc/viewModel/GjcWeighingRecordDetailsViewModel.kt index ff6343a..7204d4e 100644 --- a/module_gjc/src/main/java/com/lukouguoji/gjc/viewModel/GjcWeighingRecordDetailsViewModel.kt +++ b/module_gjc/src/main/java/com/lukouguoji/gjc/viewModel/GjcWeighingRecordDetailsViewModel.kt @@ -108,6 +108,15 @@ class GjcWeighingRecordDetailsViewModel : BaseViewModel() { launchLoadingCollect({ NetApply.api.getGjcCheckInRecordList(params) }) { onSuccess = { result -> val list = result.data ?: emptyList() + + // 为每个记录设置数据变化回调 + list.forEach { record -> + record.onDataChanged = { + // 数据变化时重新计算统计 + calculateStatistics() + } + } + recordList.value = list if (list.isEmpty()) { diff --git a/module_gjc/src/main/res/layout/activity_gjc_weighing_record_details.xml b/module_gjc/src/main/res/layout/activity_gjc_weighing_record_details.xml index 72a13e4..0c5d9f8 100644 --- a/module_gjc/src/main/res/layout/activity_gjc_weighing_record_details.xml +++ b/module_gjc/src/main/res/layout/activity_gjc_weighing_record_details.xml @@ -166,15 +166,14 @@ + android:background="#e7e7e7" + android:layout_height="1px" /> @@ -227,7 +226,7 @@ @@ -236,7 +235,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" - android:text="@{`总重量:` + viewModel.totalWeight}" + android:text="@{`总重量:` + viewModel.totalWeight}" android:textColor="@color/text_gray" android:textSize="16sp" /> @@ -245,7 +244,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="20dp" - android:text="@{`重量误差: ` + viewModel.weightError}" + android:text="@{`重量误差:` + viewModel.weightError}" android:textColor="@color/text_red" android:textSize="16sp" />