Android Navigation Drawer on multiple Activities

故事扮演 提交于 2019-11-30 06:46:59

问题


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


回答1:


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




回答2:


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")
}


来源:https://stackoverflow.com/questions/18697966/android-navigation-drawer-on-multiple-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!