This commit is contained in:
2026-04-28 14:11:48 +08:00
parent 805c9ef068
commit 827d85724e
8 changed files with 124 additions and 18 deletions

View File

@@ -1,12 +1,17 @@
package com.example.defenseapplication.home
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.util.TypedValue
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.defenseapplication.adapter.DeviceAdapter
import com.example.defenseapplication.adapter.DeviceUi
import com.example.defenseapplication.adapter.FamilyAdapter
@@ -49,6 +54,17 @@ class HomeMenuFragment : Fragment() {
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// 为 LinearLayout 设置渐变背景
setGradientBackground(
view = binding.gradientLayout,
startColor = Color.parseColor("#EBDCED"),
endColor = Color.parseColor("#CAF4FF")
)
}
private fun initView() {
binding.rvFamilyList.layoutManager = LinearLayoutManager(requireContext())
binding.rvFamilyList.adapter = familyAdapter
@@ -56,6 +72,11 @@ class HomeMenuFragment : Fragment() {
binding.rvDeviceList.layoutManager = GridLayoutManager(requireContext(), 2)
binding.rvDeviceList.adapter = deviceAdapter
if (binding.rvDeviceList.itemDecorationCount == 0) {
binding.rvDeviceList.addItemDecoration(
GridHorizontalSpaceItemDecoration(horizontalSpace = dpToPx(8))
)
}
updateDeviceList()
binding.tvCurrentFamily.text = currentFamily
@@ -94,6 +115,45 @@ class HomeMenuFragment : Fragment() {
deviceAdapter.submitList(familyDeviceMap[currentFamily].orEmpty())
}
private fun dpToPx(dp: Int): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp.toFloat(),
resources.displayMetrics
).toInt()
}
private class GridHorizontalSpaceItemDecoration(
private val horizontalSpace: Int
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val position = parent.getChildAdapterPosition(view)
if (position == RecyclerView.NO_POSITION) return
if (position % 2 == 0) {
outRect.right = horizontalSpace / 2
} else {
outRect.left = horizontalSpace / 2
}
}
}
private fun setGradientBackground(
view: View,
startColor: Int,
endColor: Int,
orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.TL_BR
) {
val gradientDrawable = GradientDrawable(orientation, intArrayOf(startColor, endColor))
gradientDrawable.gradientType = GradientDrawable.LINEAR_GRADIENT
view.background = gradientDrawable
}
override fun onDestroyView() {
super.onDestroyView()
binding.rvFamilyList.adapter = null