简体   繁体   English

多个活动上的Android导航抽屉

[英]Android Navigation Drawer on multiple Activities

有没有办法只配置导航抽屉一次,并在多个Activites上显示它?

为此,只需创建一个实现抽屉的BaseActivity类,并让所有其他活动扩展这个。

For people wanting an code example with Activities, take a look at my answer over here: https://stackoverflow.com/a/19451842/2767703 对于想要使用活动的代码示例的人,请在此处查看我的答案: 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 ). 如果你想要一个很好的过渡,我会建议:当你点击NavigationDrawer一个项目时,关闭导航抽屉并同时使用postdelayed 250(关闭NavigationDrawer所需的时间)。 Also simultaneously animate the main content's alpha to 0 with 150 milliseconds. 同时用150毫秒将主内容的alpha动画设为0。 Then when an Activity starts animate the main content's alpha to 1 with 250 milliseconds. 然后,当Activity开始动画时,主内容的alpha为250毫秒。 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 我在Google IO代码中找到了它: 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 . 顺便说一下,你还应该看看@Harish Godara给出的链接: http//www.michenux.net/android-navigation-drawer-748.html它可以与Fragments一起使用,但它有一个实现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. 它在Kotlin,但它应该明确指出。 This is all code from the BaseDrawerActivity: 这是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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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