Android UINavigationController 类似功能
在 iPhone 上,我使用导航控制器来推送和弹出视图。非常方便。
Android 中有类似的吗?
On the iPhone I use a Navigation Controller to push and pop Views from. Very handy.
Is there an equivalent in Android?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个老问题,但我相信答案已经改变了。现在可以使用 Fragments 在 android 中模仿 iOS 中的 Nav 堆栈。
http://developer.android.com/reference/android/app/Fragment.html基本上
,您不是从一个 Activity 跳转到另一个 Activity,而是停留在一个 Activity 中,该 Activity 控制 Fragment 的显示、组织和动画,每个 Fragments 都包含自己的行为,就像 iOS 中的 NavController / UIViewController 模型一样。
它还作为静态库向后兼容,因此您可以在 Honeycomb 之前的设备上实现它。
蜂窝和蜂窝策略向后兼容性
This is an old question, but I believe the answer has changed. It is now possible to imitate the Nav stack in iOS in android using Fragments.
http://developer.android.com/reference/android/app/Fragment.html
Basically instead of jumping from Activity to Activity you instead stay in one Activity that controls the display, organization, and animation of Fragments which each contain their own behavior much like the NavController / UIViewController model in iOS.
It is also backwards compatible as a static library so you can implement it on pre-Honeycomb devices.
Strategies for Honeycomb & backward compatibility
通常在 android 中,每个视图都显示在其自己的 Activity 中。您可以在应用程序基础文档中阅读有关活动的信息。要移动到新的 Activity 或视图,您可以使用 意图。
如果您还没有这样做,我强烈建议您阅读这些介绍性 Android 文档。它们不太长,并且很好地解释了基本的程序结构。
Typically in android, each view is displayed in its own Activity. You can read about activities in the application fundamentals documentation. To move to a new Activity, or view, you use an intent.
If you haven't done so yet, I'd highly recommend reading through those introductary android docs. They aren't too long, and do a good job of explaning the basic program structure.
我制作了一个 框架 (github) 来提供分层导航模式,并使用动画来提供导航感,而不是而不是每次都推出新的活动。
使用方法如下:
注意:如果您正在编辑为您提供模板的 Activity.java 文件,请删除其所有实现并将其留空。
(在 Android Studio 中如果单击 Alt + insert 并选择实现 - 方法,所有函数定义都会自动生成)。
呈现一个新的片段
您可以通过从 NavigationActivity 调用 pushFragment 方法来呈现一个新的片段(带有漂亮的动画)。
newFragment (Fragment):将要呈现的新 Fragment
animation (animationType):动画类型枚举:RIGHT_TO_LEFT、BOTTOM_TO_TOP、FLIP
showAsDetailFragmentIfPossible (boolean) :如果设置为True,用户在平板电脑中,并且您使用的是主从布局,则该Fragment将显示在详细信息Fragment中(右侧面板)!
由于您可以使用 getActivity() 方法从任何 Fragment 访问 Activity,因此您可以从当前显示的 Fragment 显示新的 Fragment。
例如,您可以将此代码放在按钮单击侦听器中:
您不必担心实现后退按钮行为。这是由 NavigationActivity 类自动处理的。
I made a Framework (github) to provide a hierarchical navigation pattern, with animations to provide sense of navigation, rather than launching new Activities every time.
Here's how to use it:
Note: If you are editing the Activity.java file that provides you the template, delete all its implementations and leave it empty.
(in Android Studio if you click Alt + insert and select implement - methods all the function definitions are automatically generated).
Presenting a new Fragment
You can present a new fragment (with a nice animation) by calling the pushFragment method from NavigationActivity.
newFragment (Fragment): New Fragment that will be presented
animation (animationType): Animation type enum: RIGHT_TO_LEFT, BOTTOM_TO_TOP, FLIP
showAsDetailFragmentIfPossible (boolean): If set as True, the user is in a Tablet, and you are using a master-detail layout, the Fragment will be shown in the detail Fragment (the panel in the right)!
Since you can access the activity from any Fragment with the getActivity() method, you can show a new Fragment from the currently displaying Fragment.
For example you can put this code within a button click listener:
You don't have to worry about implementing the back button behaviour. This is handled automatically by the NavigationActivity class.
Android 中有三种基本类型用于在 Android 中显示 UI:
Google IO 2018 引入了 导航组件 这应该会让生活更轻松。它是标准机制下的包装器。
在这里您可以找到看起来像故事板的 NavGraph 和 NavController,它们有助于导航到目的地
There are thee basic types in Android to show UI in Android:
Google IO 2018 introduced Navigation component which should make life easier. It is a wrapper under a standard mechanisms.
Here you can find NavGraph which looks like storyboard and NavController which help to navigate to destination