Android UINavigationController-like feature

﹥>﹥吖頭↗ 提交于 2019-12-31 11:36:27

问题


On the iPhone I use a Navigation Controller to push and pop Views from. Very handy.

Is there an equivalent in Android?


回答1:


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.




回答2:


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




回答3:


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:

  • Add the framework to your project as a Module
  • Add a new Java class in your project ("File - New - Java Class"). Note: If you are editing the Activity.java file that provides you the template, delete all its implementations and leave it empty.
  • Make it extend NavigationActivity
  • Implement all the NavigationActivity abstract methods

(in Android Studio if you click Alt + insert and select implement - methods all the function definitions are automatically generated).

public class NavigationTest extends NavigationActivity{
    @Override
    public Fragment firstFragment() {
        //return the first fragment that will be shown  

    }

    @Override
    public Boolean showBackButtonInFirstFragment() {
        //show back button already in the first Fragment
        //set to True if this activity is called by another Activity
        //the back button will then pop back to the previous Activity

    }

    @Override
    public Boolean showMasterDetailLayoutInTablets() {
        //set to false if you don't want a master-detail layout in tablets

    }
}

Presenting a new Fragment

You can present a new fragment (with a nice animation) by calling the pushFragment method from NavigationActivity.

public void pushFragment(Fragment newFragment, animationType animation, boolean showAsDetailFragmentIfPossible)

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:

NextFragment f = new NextFragment();
NavigationActivity nav =((NavigationActivity)getActivity());
nav.pushFragment(f,NavigationActivity.animationType.RIGHT_TO_LEFT,false);

You don't have to worry about implementing the back button behaviour. This is handled automatically by the NavigationActivity class.




回答4:


There are thee basic types in Android to show UI in Android:

  • View
  • Fragment
  • Activity

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



来源:https://stackoverflow.com/questions/3251934/android-uinavigationcontroller-like-feature

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