带标签的抽屉活动



我快疯了,需要帮助。我有一个项目,我已经实现了选项卡。现在我想放导航抽屉。我只想当我选择抽屉菜单时,我会更改选项卡,但我不知道如何引用它。我显示主活动的代码:

正如您所看到的,我已经实现了一些选项卡。以及抽屉活动。"活动"抽屉中的每个项目都调用一个片段,而每个选项卡又调用另一个片段以在ViewPager中显示。

class MainActivity : ToolbarActivity() {

private var prevBottomSelected: MenuItem? = null
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
setUpViewPager(getPagerAdapter())
setUpBottomNavigationBar()
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.drawer, menu)
return true
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}

private fun getPagerAdapter(): PagerAdapter{
val adapter = PagerAdapter(supportFragmentManager)
adapter.addFragment(InfoFragment())
adapter.addFragment(RateFragment())
adapter.addFragment(ChatFragment())
return adapter
}
private fun setUpViewPager(adapter: PagerAdapter){
viewPager.adapter = adapter
viewPager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
// Selección de menu inferior, configuracion de como aparecen seleccionados
if(prevBottomSelected == null){
bottomNavigation.menu.getItem(0).isChecked = false
} else {
prevBottomSelected!!.isChecked = false
}
bottomNavigation.menu.getItem(position).isChecked = true
prevBottomSelected = bottomNavigation.menu.getItem(position)
}
})
}
private fun setUpBottomNavigationBar(){
bottomNavigation.setOnNavigationItemSelectedListener { item ->
when (item.itemId){
R.id.bottom_nav_info -> {
viewPager.currentItem = 0; true
}
R.id.bottom_nav_rates -> {
viewPager.currentItem = 1; true
}
R.id.bottom_nav_chat -> {
viewPager.currentItem = 2; true
}
else -> false
}
}
}}

输入PageAdapter代码。我需要当我在抽屉中按下nav_home时,不同的片段和项目会出现在选项卡中。

class PagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
private val fragmentList = ArrayList<Fragment>()
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
override fun getCount(): Int {
return fragmentList.size
}
fun addFragment(fragment: Fragment){
fragmentList.add(fragment)}}

由于PagerAdapter是抽象的,您可能有我们看不到的它的实现。我想这就是问题的原因。如果是这种情况,请尝试为寻呼机适配器编写以下代码:

class pagerAdapter(fm: FragmentManager): FragmentStatePagerAdapter(fm) {
override fun getItem(position: Int): Fragment {
return when (position) {
0 -> InfoFragment()
1 -> RateFragment()
2 -> ChatFragment()
else -> throw RuntimeException("Unsupported fragment position")
}
}
override fun getCount() = 3
}

相关内容

  • 没有找到相关文章

最新更新