Java 多个活动的 Android 导航抽屉
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18697966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Navigation Drawer on multiple Activities
提问by Deniz Celebi
Is there a way to configure only one time the Navigation Drawer, and the display it on multiple Activites?
有没有办法只配置一次导航抽屉,并在多个活动上显示它?
采纳答案by Harish Godara
For this just create a BaseActivity class that implements the drawer, and let all your other activities extend this one.
为此,只需创建一个实现抽屉的 BaseActivity 类,并让所有其他活动扩展这个类。
回答by Kevin van Mierlo
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
). 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
如果您想要一个不错的过渡,我建议这样做:当您单击NavigationDrawer
关闭导航抽屉中的项目并同时使用带有 250 的 postdelayed(关闭 所需的时间NavigationDrawer
)时。同时以 150 毫秒将主要内容的 alpha 动画化为 0。然后,当 Activity 开始时,主要内容的 alpha 将在 250 毫秒内设置为 1。这提供了一个很好的过渡。我在 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.htmlIt 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. This is all code from the BaseDrawerActivity:
由于这里的某些链接已失效,因此我在上一个项目中使用它来获取动画。它在 Kotlin 中,但它应该清楚地说明这一点。这是来自 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")
}