| | |
| | | import androidx.lifecycle.ViewModelProvider |
| | | import androidx.recyclerview.widget.LinearLayoutManager |
| | | import androidx.recyclerview.widget.RecyclerView |
| | | import com.bumptech.glide.Glide |
| | | 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 |
| | | |
| | |
| | | |
| | | //通过 ViewModelProvider 获取 HomeViewModel 的实例,以便在视图中使用。 |
| | | homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java) |
| | | |
| | | // 加载广告图片 |
| | | //loadAdvertisements() |
| | | //调用这个方法来设置 RecyclerView用于设置 RecyclerView 的布局和适配器。 |
| | | setupRecyclerViews() |
| | | setupTabSwitching() |
| | | //调用这个方法来观察 ViewModel 中的数据变化 |
| | | observeViewModelData() |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | // // 财务列表 |
| | | // 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) |
| | |
| | | super.onDestroyView() |
| | | _binding = null |
| | | } |
| | | |
| | | private fun loadAdvertisements() { |
| | | // 使用 Glide 加载网络图片 |
| | | Glide.with(this) |
| | | .load("http://192.168.1.235:9999/advertisement/up.png") |
| | | .into(binding.adBanner) |
| | | |
| | | Glide.with(this) |
| | | .load("http://192.168.1.235:9999/advertisement/down.png") |
| | | .into(binding.bottomAdBanner) |
| | | } |
| | | } |