You can build your menu items dynamically.
Fist step - find your navigation view in your Activity
private lateinit var navView: NavigationView
...
navView = findViewById(R.id.nav_view)
Second step - inflate navigation view with items like this
pages.forEach { page ->
navView.menu.add(0, page.navId, 0, page.name)
.apply {
setNavItemIcon(this, page.faIcon, page.customIcon)
isCheckable = true
}
}
navView.menu.setGroupCheckable(0, true, true)
where pages - list of navigation item models, fetched from db (in your case API) and 'page' is the model of the menu item.
Third step - use setNavigationItemSelectedListener for handling clicks
navView.setNavigationItemSelectedListener {
onMenuItemSelected(it, pages)
}
Also you maybe you'll need to use navView.menu.clear() to clear menu and inflate it again.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…