首页
This commit is contained in:
@@ -5,13 +5,39 @@ import androidx.fragment.app.Fragment
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import com.example.defenseapplication.R
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.example.defenseapplication.adapter.DeviceAdapter
|
||||
import com.example.defenseapplication.adapter.DeviceUi
|
||||
import com.example.defenseapplication.adapter.FamilyAdapter
|
||||
import com.example.defenseapplication.databinding.HomeMenuFragmentBinding
|
||||
|
||||
class HomeMenuFragment : Fragment() {
|
||||
// ViewBinding
|
||||
private var _binding: HomeMenuFragmentBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
private val familyAdapter by lazy { FamilyAdapter(::onFamilySelected) }
|
||||
private val deviceAdapter by lazy { DeviceAdapter() }
|
||||
|
||||
private var isFamilyExpanded = false
|
||||
private var currentFamily = "我的家庭"
|
||||
|
||||
private val familyList = listOf("我的家庭", "工作室", "父母家")
|
||||
private val familyDeviceMap = mapOf(
|
||||
"我的家庭" to listOf(
|
||||
DeviceUi("智能挂式防御机", true),
|
||||
DeviceUi("智能防御机", false),
|
||||
DeviceUi("智能轨道防御机", false)
|
||||
),
|
||||
"工作室" to listOf(
|
||||
DeviceUi("走廊防御机", true),
|
||||
DeviceUi("门禁防御机", true)
|
||||
),
|
||||
"父母家" to listOf(
|
||||
DeviceUi("客厅防御机", false),
|
||||
DeviceUi("阳台防御机", true)
|
||||
)
|
||||
)
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
@@ -19,11 +45,59 @@ class HomeMenuFragment : Fragment() {
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
_binding = HomeMenuFragmentBinding.inflate(inflater, container, false)
|
||||
initView()
|
||||
return binding.root
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
binding.rvFamilyList.layoutManager = LinearLayoutManager(requireContext())
|
||||
binding.rvFamilyList.adapter = familyAdapter
|
||||
familyAdapter.submitList(familyList, currentFamily)
|
||||
|
||||
binding.rvDeviceList.layoutManager = GridLayoutManager(requireContext(), 2)
|
||||
binding.rvDeviceList.adapter = deviceAdapter
|
||||
updateDeviceList()
|
||||
|
||||
binding.tvCurrentFamily.text = currentFamily
|
||||
binding.familyHeaderLayout.setOnClickListener {
|
||||
toggleFamilyList()
|
||||
}
|
||||
binding.familyPopupMask.setOnClickListener {
|
||||
if (isFamilyExpanded) {
|
||||
toggleFamilyList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun toggleFamilyList() {
|
||||
isFamilyExpanded = !isFamilyExpanded
|
||||
val visibility = if (isFamilyExpanded) View.VISIBLE else View.GONE
|
||||
binding.familyPopupMask.visibility = visibility
|
||||
binding.familyPopupCard.visibility = visibility
|
||||
binding.ivFamilyArrow.animate()
|
||||
.rotation(if (isFamilyExpanded) 180f else 0f)
|
||||
.setDuration(200L)
|
||||
.start()
|
||||
}
|
||||
|
||||
private fun onFamilySelected(familyName: String) {
|
||||
currentFamily = familyName
|
||||
binding.tvCurrentFamily.text = familyName
|
||||
familyAdapter.submitList(familyList, currentFamily)
|
||||
updateDeviceList()
|
||||
if (isFamilyExpanded) {
|
||||
toggleFamilyList()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateDeviceList() {
|
||||
deviceAdapter.submitList(familyDeviceMap[currentFamily].orEmpty())
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
binding.rvFamilyList.adapter = null
|
||||
binding.rvDeviceList.adapter = null
|
||||
_binding = null // 避免内存泄漏
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user