cloudroam
10 天以前 04b138d3836e03c9adbcbd367fd71d92905c5206
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.example.firstapp.adapter
 
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.firstapp.R
import com.example.firstapp.databinding.ItemFinanceBinding
import com.example.firstapp.databinding.ItemFinanceGroupBinding
import com.example.firstapp.databinding.ItemFinancePackageHomeBinding
import com.example.firstapp.model.FinanceGroup
import com.example.firstapp.model.FinancePackage
 
class FinanceAdapter :
    ListAdapter<FinanceGroup, FinanceAdapter.ViewHolder>(FinanceGroupDiffCallback()) {
 
    private var onPackageClickListener: (FinanceGroup, FinancePackage) -> Unit = { _, _ -> }
 
    fun setOnPackageClickListener(listener: (FinanceGroup, FinancePackage) -> Unit) {
        onPackageClickListener = listener
    }
 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemFinanceGroupBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(binding)
    }
 
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val group = getItem(position)
        holder.bind(group)
        holder.setOnPackageClickListener(onPackageClickListener)
    }
 
 
    inner class ViewHolder(private val binding: ItemFinanceGroupBinding) :
        RecyclerView.ViewHolder(binding.root) {
        private val packagesAdapter = FinancePackageHomeAdapter { pack ->
            currentGroup?.let { group ->
                onPackageClickListener(group, pack)
            }
        }
        private var currentGroup: FinanceGroup? = null
 
        init {
            // 设置固定高度和禁用嵌套滚动来解决滑动问题
            binding.rvPackages.apply {
                layoutManager = object : LinearLayoutManager(context) {
                    override fun canScrollVertically(): Boolean {
                        // 禁用内部RecyclerView的垂直滚动
                        return false
                    }
                    
                    // 确保测量所有子项,防止部分内容不可见
                    override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) {
                        try {
                            super.onLayoutChildren(recycler, state)
                        } catch (e: IndexOutOfBoundsException) {
                            // 捕获可能的异常,防止崩溃
                        }
                    }
                }
                adapter = packagesAdapter
                // 禁用嵌套滚动,让外部RecyclerView处理所有滚动
                isNestedScrollingEnabled = false
                // 启用回收视图缓存
                setItemViewCacheSize(20)
                setHasFixedSize(true)
            }
        }
 
        fun bind(group: FinanceGroup) {
            currentGroup = group
            binding.tvStationName.text = group.stationName
            binding.tvPackageCount.text = "共${group.packages.size}笔账单"
            
            // 确保所有数据都被更新
            packagesAdapter.submitList(null)
            packagesAdapter.submitList(group.packages)
            
            // 请求布局刷新
            binding.rvPackages.requestLayout()
        }
 
        fun setOnPackageClickListener(listener: (FinanceGroup, FinancePackage) -> Unit) {
            // 这个方法可以移除,因为我们在构造 ExpressPackageHomeAdapter 时已经处理了点击事件
            // 或者保留这个方法但不做任何操作
        }
    }
}
 
// 首页使用的包裹适配器 - 简化版本
class FinancePackageHomeAdapter(private val onPackageClick: (FinancePackage) -> Unit) :
    ListAdapter<FinancePackage, FinancePackageHomeAdapter.ViewHolder>(FinancePackageDiffCallback()) {
 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemFinancePackageHomeBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(binding)
    }
 
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val pack = getItem(position)
        holder.bind(pack)
    }
    
    // 防止部分内容不显示
    override fun getItemCount(): Int {
        return currentList.size
    }
 
    inner class ViewHolder(private val binding: ItemFinancePackageHomeBinding) :
        RecyclerView.ViewHolder(binding.root) {
 
        init {
            binding.root.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    val pack = getItem(position)
                    onPackageClick(pack)
                }
            }
        }
 
        fun bind(pack: FinancePackage) {
            binding.tvCompany.text = pack.company
            binding.tvCreateTime.text = pack.createTime
            binding.tvTrackingNumber.text = pack.trackingNumber
        }
    }
}
 
// 取件页面使用的包裹适配器
class FinancePackageAdapter(private val onPackagePickup: (FinancePackage) -> Unit = { _ -> }) :
    ListAdapter<FinancePackage, FinancePackageAdapter.ViewHolder>(FinancePackageDiffCallback()) {
 
    private var onPackageClickListener: (FinancePackage) -> Unit = {}
    private var stationName: String = ""
 
    fun setStationInfo(station: String) {
        stationName = station
    }
 
    fun setOnPackageClickListener(listener: (FinancePackage) -> Unit) {
        onPackageClickListener = listener
    }
 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemFinanceBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(binding)
    }
 
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val pack = getItem(position)
        holder.bind(pack)
    }
    
    // 防止部分内容不显示
    override fun getItemCount(): Int {
        return currentList.size
    }
 
    inner class ViewHolder(private val binding: ItemFinanceBinding) :
        RecyclerView.ViewHolder(binding.root) {
 
        init {
            binding.ivPackageStatus.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    val pack = getItem(position)
                    onPackagePickup(pack)
                }
            }
 
            binding.root.setOnClickListener(null)
        }
 
        fun bind(pack: FinancePackage) {
            binding.tvPackageId.text = pack.id.toString()
            binding.tvCompany.text = pack.company
            binding.tvCreateTime.text = pack.createTime
            binding.tvTrackingNumber.text = pack.trackingNumber
            binding.ivPackageStatus.setImageResource(
//                if (pack.isPickedUp) R.drawable.circle_checked
//                else R.drawable.circle
                R.drawable.circle
            )
        }
    }
}
 
 
private class FinanceGroupDiffCallback : DiffUtil.ItemCallback<FinanceGroup>() {
    override fun areItemsTheSame(oldItem: FinanceGroup, newItem: FinanceGroup): Boolean {
        return oldItem.stationName == newItem.stationName
    }
 
    override fun areContentsTheSame(oldItem: FinanceGroup, newItem: FinanceGroup): Boolean {
        return oldItem == newItem
    }
}
 
private class FinancePackageDiffCallback : DiffUtil.ItemCallback<FinancePackage>() {
    override fun areItemsTheSame(oldItem: FinancePackage, newItem: FinancePackage): Boolean {
        return oldItem.trackingNumber == newItem.trackingNumber
    }
 
    override fun areContentsTheSame(oldItem: FinancePackage, newItem: FinancePackage): Boolean {
        return oldItem == newItem
    }
}