Naviagtion initial use of components

Navigate components (navigation components), mainly to jump between pages navigation, Fragment page.

Learning Demo: the Navigate

ps: Now these codes are kotlin write, really not to learn a little kotlin, can not see these things really are. There should be an error, please criticism.

1, adding a dependency:
implementation "androidx.navigation:navigation-fragment-ktx:2.1.0"
implementation "androidx.navigation:navigation-ui-ktx:2.1.0"
2, the basic steps of using:

Create a navigation file, create a fragment, fragment connection process, setting activity in the navigation fragment connections, set to jump between the fragment (animated action can be set between the jump)

details as follows:

  • 1. In the res folder, create a folder navigation. Next, create a resource of xml file.

  • 2. Go to the file, enter the design mode. Here, you can create a fragment class. Then you can design your fragment of the process. Using a mouse connected to a jump code for each fragment, in response to the automatically generated.

    (Clicks, you can create a new frgament. In navigate.xml folder below you can see a new generation of fragment node)

  • 3.Activity fragment in the layout setting, which is linked to the above xml file, (name, defaultNavHost, navGraph attributes) are set as follows:

    <fragment
     android:id="@+id/my_nav_host_fragment"        
     android:name="androidx.navigation.fragment.NavHostFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:defaultNavHost="true"
     app:navGraph="@navigation/mobile_navigation" />
  • 4. The fragment in the class, a jump is provided:

    view.findViewById<Button(R.id.navigate_destination_button)?.setOnClickListener {
      findNavController().navigate(R.id.flow_step_one_dest, null)
    }
    
    //or
    Navigation.createNavigateOnClickListener(R.id.flow_step_one_dest)
  • 5. Skip transition animation, following settings are available:

    <fragment
          android:id="@+id/home_dest" 
        android:name="****"
        android:label="@string/home"
        tools:layout="@layout/home_fragment">
    
        <action
            android:id="@+id/next_action"
            app:destination="@+id/flow_step_one_dest"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right" />
    
    </fragment>

    ** The basic usage is the case

Guess you like

Origin www.cnblogs.com/wisdomzhang/p/12021374.html