Android Navigation Drawer on multiple Activities

后端 未结 2 1297
北海茫月
北海茫月 2020-12-14 19:07

Is there a way to configure only one time the Navigation Drawer, and the display it on multiple Activites?

相关标签:
2条回答
  • 2020-12-14 19:32

    For people wanting an code example with Activities, take a look at my answer over here: https://stackoverflow.com/a/19451842/2767703

    If you want a nice transition I would suggest this: When you click on an item in the NavigationDrawer close the navigation drawer and simultaneously use postdelayed with 250 (time it takes to close the NavigationDrawer). Also simultaneously animate the main content's alpha to 0 with 150 milliseconds. Then when an Activity starts animate the main content's alpha to 1 with 250 milliseconds. This gives a great transition. I found it in the Google IO code: https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java

    By the way, you should also look at the link @Harish Godara gave: http://www.michenux.net/android-navigation-drawer-748.html It works with Fragments but it has a nice way of implementing the NavigationDrawer.

    Edit

    Since some links are dead here is what I used in my last project to get the animation. It is in Kotlin, but it should make the point clear. This is all code from the BaseDrawerActivity:

    private val NAVDRAWER_LAUNCH_DELAY = 250L
    private val MAIN_CONTENT_FADEOUT_DURATION = 150L
    private val MAIN_CONTENT_FADEIN_DURATION = 250L
    

    -

    private var shouldAnimate:Boolean
        set(value) { intent.putExtra("animateTransition", value) }
        get() = intent.getBooleanExtra("animateTransition", false)
    

    -

    private fun changeDrawerItem(newClass: Class<*>) {
        runDelayed(NAVDRAWER_LAUNCH_DELAY, {
            startActivity(Intent(this, newClass).apply {
                addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
                putExtra("animateTransition", true)
                putExtra("selectedNav", selectedNavigationItem.name)
            })
            overridePendingTransition(0, 0)
        })
    
        mainContent.animate()?.alpha(0f)?.duration = MAIN_CONTENT_FADEOUT_DURATION
    }
    

    -

    override fun onStart() {
        super.onStart()
    
        if(shouldAnimate) {
            mainContent.alpha = 0f
            mainContent.animate()?.alpha(1f)?.duration = MAIN_CONTENT_FADEIN_DURATION
        } else {
            mainContent.alpha = 1f
        }
    
        val selectedNav = intent.getStringExtra("selectedNav")
        if(selectedNav != null) {
            selectedNavigationItem = DrawerItem.valueOf(selectedNav)
        }
    }
    

    -

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
    
        if(shouldAnimate) {
            overridePendingTransition(0, 0)
        }
    }
    

    -

    override fun onResume() {
        super.onResume()
        intent.removeExtra("animateTransition")
    }
    
    0 讨论(0)
  • 2020-12-14 19:36

    For this just create a BaseActivity class that implements the drawer, and let all your other activities extend this one.

    0 讨论(0)
提交回复
热议问题