zhujie
9 天以前 88b00f3fc74446a1727c93722c7b64179b45a9db
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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}笔账单"
 
            // 根据驿站名称设置相应的图标
            val stationName = group.stationName.lowercase()
            val stationIconResId = when {
                stationName.isEmpty() || stationName == "未知" -> R.drawable.finance_huankuan_normal
                stationName.contains("安逸花") -> R.drawable.finance_anyihua
                stationName.contains("房贷") -> R.drawable.finance_fangdai
                stationName.contains("还款") -> R.drawable.finance_huankuan
                stationName.contains("花呗") -> R.drawable.finance_huabei
                stationName.contains("借呗") -> R.drawable.finance_jiebei
                stationName.contains("京东白条") -> R.drawable.finance_jingdongbaitiao
                stationName.contains("京东金条") -> R.drawable.finance_jingdongjintiao
                stationName.contains("汽车分期") -> R.drawable.finance_qichefenqi
                stationName.contains("省呗") -> R.drawable.finance_shenbei
                stationName.contains("信用卡") -> R.drawable.finance_xingyongka
                stationName.contains("支付宝") -> R.drawable.finance_zhifubao
                else -> R.drawable.finance_huankuan_normal
            }
 
            binding.ivStationIcon.setImageResource(stationIconResId)
 
            // 确保所有数据都被更新
            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
            val companyName = pack.company.lowercase()
            val logoResId = when {
                companyName.isEmpty() || companyName == "未知" -> R.drawable.finance_income_bank_normal
                companyName.contains("工商银行") -> R.drawable.bank_gongshang
                companyName.contains("光大银行") -> R.drawable.bank_guangda
                companyName.contains("国家开发银行") -> R.drawable.bank_guojiakaifa
                companyName.contains("恒丰银行") -> R.drawable.bank_hengfeng
                companyName.contains("华夏银行") -> R.drawable.bank_huaxia
                companyName.contains("花呗分期") -> R.drawable.bank_huabeifenqi
                companyName.contains("建设银行") -> R.drawable.bank_jianshe
                companyName.contains("交通银行") -> R.drawable.bank_jiaotong
                companyName.contains("民生银行") -> R.drawable.bank_mingsheng
                companyName.contains("南京银行") -> R.drawable.bank_nanjing
                companyName.contains("农商银行") -> R.drawable.bank_nongshang
                companyName.contains("农业银行") -> R.drawable.bank_nongye
                companyName.contains("平安银行") -> R.drawable.bank_pingan
                companyName.contains("浦发银行") -> R.drawable.bank_pufa
                companyName.contains("苏州银行") -> R.drawable.bank_suzhou
                companyName.contains("邮储银行") -> R.drawable.bank_youzheng
                companyName.contains("招商银行") -> R.drawable.bank_zhaoshang
                companyName.contains("中国银行") -> R.drawable.bank_zhongguo
                companyName.contains("中信银行") -> R.drawable.bank_zhongxin
                companyName.contains("中原银行") -> R.drawable.bank_zhongyuan
                companyName.contains("支付宝") -> R.drawable.finance_zhifubao
                else -> R.drawable.finance_income_bank_normal
            }
            binding.ivCompanyLogo.setImageResource(logoResId)
        }
    }
}
 
// 取件页面使用的包裹适配器
//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
//            )
//            // 根据快递公司名称设置相应的图标
//            val companyName = pack.company.lowercase()
//            val logoResId = when {
//                companyName.isEmpty() || companyName == "未知" -> R.drawable.finance_income_bank_normal
//                companyName.contains("工商银行") -> R.drawable.bank_gongshang
//                companyName.contains("光大银行") -> R.drawable.bank_guangda
//                companyName.contains("国家开发银行") -> R.drawable.bank_guojiakaifa
//                companyName.contains("恒丰银行") -> R.drawable.bank_hengfeng
//                companyName.contains("华夏银行") -> R.drawable.bank_huaxia
//                companyName.contains("花呗分期") -> R.drawable.bank_huabeifenqi
//                companyName.contains("建设银行") -> R.drawable.bank_jianshe
//                companyName.contains("交通银行") -> R.drawable.bank_jiaotong
//                companyName.contains("民生银行") -> R.drawable.bank_mingsheng
//                companyName.contains("南京银行") -> R.drawable.bank_nanjing
//                companyName.contains("农商银行") -> R.drawable.bank_nongshang
//                companyName.contains("农业银行") -> R.drawable.bank_nongye
//                companyName.contains("平安银行") -> R.drawable.bank_pingan
//                companyName.contains("浦发银行") -> R.drawable.bank_pufa
//                companyName.contains("苏州银行") -> R.drawable.bank_suzhou
//                companyName.contains("邮储银行") -> R.drawable.bank_youzheng
//                companyName.contains("招商银行") -> R.drawable.bank_zhaoshang
//                companyName.contains("中国银行") -> R.drawable.bank_zhongguo
//                companyName.contains("中信银行") -> R.drawable.bank_zhongxin
//                companyName.contains("中原银行") -> R.drawable.bank_zhongyuan
//                else -> R.drawable.finance_income_bank_normal
//            }
//
//            binding.ivCompanyLogo.setImageResource(logoResId)
//
//        }
//    }
//}
 
 
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
    }
}