简体   繁体   中英

Navigation Drawer Activity change fragment from external button

So I have set up the default Navigation Drawer Activity in Android Studio. I also added some buttons in the default navigation activity XML. The default navigation menu buttons obviously change the fragment, but I was wondering how I could also change the fragments via the buttons that I have added along the bottom? The reason I am having trouble is that the default nagivation menu buttons are part of a navGraph, whereas my buttons are not, thanks!

[ 在此处输入图片说明 ][ 在此处输入图片说明 ] 在此处输入图片说明

All you need to do is set onClickListeners on the button.

For example:

val postButton = findViewById<Button>(R.id.post_button)

postButton.setOnClickListener {
    findNavController().navigate(R.id.post_fragment)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM