| | |
| | | package com.example.firstapp.ui.home |
| | | |
| | | import android.content.BroadcastReceiver |
| | | import android.content.Context |
| | | import android.content.Intent |
| | | import android.content.IntentFilter |
| | | import android.os.Bundle |
| | | import android.view.LayoutInflater |
| | | import android.view.View |
| | | import android.view.ViewGroup |
| | | import android.widget.TextView |
| | | import androidx.core.content.ContextCompat |
| | | import androidx.fragment.app.Fragment |
| | | import androidx.lifecycle.ViewModelProvider |
| | | import androidx.recyclerview.widget.LinearLayoutManager |
| | | import androidx.recyclerview.widget.RecyclerView |
| | | import com.example.firstapp.R |
| | | import com.example.firstapp.activity.PickupActivity |
| | | import com.example.firstapp.adapter.ExpressAdapter |
| | | import com.example.firstapp.adapter.FinanceAdapter |
| | | import com.example.firstapp.core.Core |
| | | import com.example.firstapp.databinding.FragmentHomeBinding |
| | | |
| | |
| | | |
| | | private lateinit var homeViewModel: HomeViewModel |
| | | private lateinit var expressAdapter: ExpressAdapter |
| | | // private lateinit var financeAdapter: FinanceAdapter |
| | | private lateinit var financeAdapter: FinanceAdapter |
| | | // private lateinit var memorialAdapter: MemorialAdapter |
| | | private lateinit var dataUpdateReceiver: BroadcastReceiver |
| | | |
| | | //onCreateView这个方法创建后被调用,通常是初始化视图组件和观察者 |
| | | override fun onCreateView( |
| | |
| | | |
| | | //调用这个方法来设置 RecyclerView用于设置 RecyclerView 的布局和适配器。 |
| | | setupRecyclerViews() |
| | | setupTabSwitching() |
| | | //调用这个方法来观察 ViewModel 中的数据变化 |
| | | observeViewModelData() |
| | | } |
| | | |
| | | override fun onCreate(savedInstanceState: Bundle?) { |
| | | super.onCreate(savedInstanceState) |
| | | |
| | | // 创建广播接收器 |
| | | dataUpdateReceiver = object : BroadcastReceiver() { |
| | | override fun onReceive(context: Context, intent: Intent) { |
| | | if (intent.action == "com.example.firstapp.DATA_UPDATED") { |
| | | // 收到数据更新广播时重新加载数据 |
| | | homeViewModel.loadExpressData() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private fun setupRecyclerViews() { |
| | | // 快递列表 |
| | | //layoutManager = LinearLayoutManager(context):设置 RecyclerView 的布局管理器为线性布局管理器,表示列表是垂直排列的。 |
| | | //创建一个 ExpressAdapter 的实例,用于提供 RecyclerView 的数据。 |
| | | //将适配器设置给 RecyclerView,以便显示数据。 |
| | | binding.expressRecycler.apply { |
| | | layoutManager = LinearLayoutManager(context) |
| | | expressAdapter = ExpressAdapter() |
| | | adapter = expressAdapter |
| | | |
| | | // 设置点击监听 |
| | | expressAdapter.setOnPackageClickListener { group, pack -> |
| | | // 跳转到取件页面 |
| | | val intent = Intent(requireContext(), PickupActivity::class.java).apply { |
| | | putExtra("station_name", group.stationName) |
| | | putExtra("company", pack.company) |
| | | } |
| | | startActivity(intent) |
| | | } |
| | | } |
| | | |
| | | // // 财务列表 |
| | | // binding.financeRecycler.apply { |
| | | // layoutManager = LinearLayoutManager(context) |
| | | // financeAdapter = FinanceAdapter() |
| | | // adapter = financeAdapter |
| | | // } |
| | | // 财务列表 |
| | | binding.financeRecycler.apply { |
| | | layoutManager = LinearLayoutManager(context) |
| | | financeAdapter = FinanceAdapter() |
| | | adapter = financeAdapter |
| | | |
| | | // 设置初始状态 - 添加这行 |
| | | binding.financeContent.visibility = View.GONE |
| | | |
| | | // 设置点击监听 |
| | | financeAdapter.setOnPackageClickListener { group, pack -> |
| | | // 跳转到取件页面 |
| | | val intent = Intent(requireContext(), PickupActivity::class.java).apply { |
| | | putExtra("station_name", group.stationName) |
| | | putExtra("company", pack.company) |
| | | } |
| | | startActivity(intent) |
| | | } |
| | | } |
| | | // |
| | | // // 纪念日列表 |
| | | // binding.memorialRecycler.apply { |
| | |
| | | // memorialAdapter = MemorialAdapter() |
| | | // adapter = memorialAdapter |
| | | // } |
| | | } |
| | | |
| | | private fun setupTabSwitching() { |
| | | binding.apply { |
| | | // 设置初始状态 |
| | | tabExpress.setTextColor(ContextCompat.getColor(requireContext(), R.color.tab_selected)) |
| | | tabFinance.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray)) |
| | | others.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray)) |
| | | |
| | | // 快递标签点击事件 |
| | | tabExpress.setOnClickListener { |
| | | expressContent.visibility = View.VISIBLE |
| | | financeContent.visibility = View.GONE |
| | | tabExpress.setTextColor(ContextCompat.getColor(requireContext(), R.color.tab_selected)) |
| | | tabFinance.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray)) |
| | | others.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray)) |
| | | tabExpress.textSize = 16f |
| | | tabFinance.textSize = 14f |
| | | others.textSize = 14f |
| | | } |
| | | |
| | | // 财务标签点击事件 |
| | | tabFinance.setOnClickListener { |
| | | expressContent.visibility = View.GONE |
| | | financeContent.visibility = View.VISIBLE |
| | | tabExpress.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray)) |
| | | tabFinance.setTextColor(ContextCompat.getColor(requireContext(), R.color.tab_selected)) |
| | | others.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray)) |
| | | tabExpress.textSize = 14f |
| | | tabFinance.textSize = 16f |
| | | others.textSize = 14f |
| | | |
| | | // 在切换到财务标签时加载数据 - 添加这行 |
| | | homeViewModel.loadFinanceData() |
| | | } |
| | | |
| | | // 其他标签点击事件 |
| | | others.setOnClickListener { |
| | | expressContent.visibility = View.GONE |
| | | financeContent.visibility = View.GONE |
| | | tabExpress.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray)) |
| | | tabFinance.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray)) |
| | | others.setTextColor(ContextCompat.getColor(requireContext(), R.color.tab_selected)) |
| | | tabExpress.textSize = 14f |
| | | tabFinance.textSize = 14f |
| | | others.textSize = 16f |
| | | } |
| | | } |
| | | } |
| | | |
| | | //这个方法用于观察 homeViewModel 中的 expressItems 数据。 |
| | |
| | | expressAdapter.submitList(items) |
| | | } |
| | | |
| | | // homeViewModel.financeItems.observe(viewLifecycleOwner) { items -> |
| | | // financeAdapter.submitList(items) |
| | | // } |
| | | homeViewModel.financeItems.observe(viewLifecycleOwner) { items -> |
| | | financeAdapter.submitList(items) |
| | | } |
| | | // |
| | | // homeViewModel.memorialItems.observe(viewLifecycleOwner) { items -> |
| | | // memorialAdapter.submitList(items) |
| | | // } |
| | | } |
| | | |
| | | override fun onResume() { |
| | | super.onResume() |
| | | // 使用 ContextCompat 注册广播接收器,并指定 RECEIVER_NOT_EXPORTED 标志 |
| | | ContextCompat.registerReceiver( |
| | | requireContext(), |
| | | dataUpdateReceiver, |
| | | IntentFilter("com.example.firstapp.DATA_UPDATED"), |
| | | ContextCompat.RECEIVER_NOT_EXPORTED |
| | | ) |
| | | // 加载数据 |
| | | homeViewModel.loadExpressData() |
| | | } |
| | | |
| | | override fun onPause() { |
| | | super.onPause() |
| | | try { |
| | | // 取消注册广播接收器 |
| | | requireContext().unregisterReceiver(dataUpdateReceiver) |
| | | } catch (e: Exception) { |
| | | // 处理可能的异常 |
| | | e.printStackTrace() |
| | | } |
| | | } |
| | | |
| | | override fun onDestroyView() { |
| | | super.onDestroyView() |
| | | _binding = null |