Jectpack之navigation

網上的文章說的繞的都很暈,希望我這篇文章,你能拿來直接用。不會說過多的原理。

一、navigation的作用

導航的作用,管理fragment如何顯示,如何跳轉。傳統的fragment顯示,
有三種方式:

  1. 直接寫在xml中
  2. 用FragmentTransaction的commit
  3. 用viewpager

現在多了一種navigation來管理fragment

二、navigation的使用

navigation的使用就類似一張地圖setGraph,地圖上有目的地nav_simplexml,然後開車getNavController駛去
上面一句話就是使用的根本了,好好體會

步驟1、引入依賴
//Navigation
implementation "android.arch.navigation:navigation-fragment:1.0.0-alpha02"
implementation "android.arch.navigation:navigation-ui:1.0.0-alpha02"
步驟2、新建地圖,即nav_simplexml

在res目錄下新建navigation文件夾,再建navigation.xml
如下圖:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/nav_simple_first_frag">
<fragment
    android:id="@+id/nav_simple_first_frag"
    android:name="com.ce.navigationtest.simple.FirstFragment"
    android:label="first frag"
    tools:layout="@layout/fragment_simple_first">
    <!--<action
        android:id="@+id/action_nav_first_frag_to_nav_second_frag"
        app:destination="@id/nav_simple_second_frag"
        app:enterAnim="@anim/fade_in"
        app:exitAnim="@anim/fade_out"
        app:popEnterAnim="@anim/fade_in"
        app:popExitAnim="@anim/fade_out"/>-->
    <action
        android:id="@+id/action_nav_first_frag_to_nav_second_frag"
        app:destination="@id/nav_simple_second_frag" />
    <argument android:name="data" app:type="string" />
</fragment>
<fragment
    android:id="@+id/nav_simple_second_frag"
    android:name="com.ce.navigationtest.simple.SecondFragment"
    android:label="second frag"
    tools:layout="@layout/fragment_simple_second">
    <action
        android:id="@+id/action_nav_second_frag_to_nav_third_frag"
        app:destination="@id/nav_simple_third_frag" />
    <argument android:name="booleanData" app:type="boolean" />
    <!--<argument android:name="byteData" app:type="byte" />-->
    <!--<argument android:name="charData" app:type="char" />-->
    <!--<argument android:name="shortData" app:type="short" />-->
    <argument android:name="intData" app:type="integer" />
    <!--<argument android:name="longData" app:type="long" />-->
    <argument android:name="floatData" app:type="float" />
    <!--<argument android:name="doubleData" app:type="double" />-->
    <argument android:name="stringData" app:type="string" />
</fragment>
<fragment
    android:id="@+id/nav_simple_third_frag"
    android:name="com.ce.navigationtest.simple.ThirdFragment"
    android:label="third frag"
    tools:layout="@layout/fragment_simple_third">
    <action
        android:id="@+id/action_nav_third_frag_to_nav_fourth_frag"
        app:destination="@id/nav_simple_fourth_frag" />
</fragment>
<fragment
    android:id="@+id/nav_simple_fourth_frag"
    android:name="com.ce.navigationtest.simple.FourthFragment"
    android:label="fourth frag"
    tools:layout="@layout/fragment_simple_fourth">
</fragment>
</navigation>

注:用不到傳值的可以刪掉action

步驟3、Activity的配置

activity_simple.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".simple.SimpleActivity">

<fragment
    android:id="@+id/frag_nav_simple"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true" />
</android.support.constraint.ConstraintLayout>

NavHostFragment是地圖的fragment,它上面裝載着其他fragment

步驟4、具體的使用
public class SimpleActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
     


        NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.frag_nav_simple);
        NavGraph navSimple = navHostFragment.getNavController().getNavInflater().inflate(R.navigation.nav_simple);
        NavDestination firstFragDestination = navSimple.findNode(R.id.nav_simple_first_frag);
        FirstFragmentArgs fragmentArgs = new FirstFragmentArgs.Builder("給 First 的數據").build();
        firstFragDestination.setDefaultArguments(fragmentArgs.toBundle());
        navHostFragment.getNavController().setGraph(navSimple);
    }

    @Override
    public boolean onSupportNavigateUp() {
        return Navigation.findNavController(this, R.id.frag_nav_simple).navigateUp();
    }
}  

至此示例代碼就完成了,FirstFragment等fragment換成你的即可,大部分代碼參考網上。重點理解地圖,目的地,如何控制跳轉的概念。

我也解決了navigation的fragment每次切換重建的問題,有需要私聊我。要demo也是。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章