Navigation(1)—— 使用

367 阅读1分钟

如果文章有问题,请及时指出

一 使用Navigation进行导航

1 添加依赖

dependencies {
  def nav_version = "2.3.0"

  // Java language implementation
  implementation "androidx.navigation:navigation-fragment:$nav_version"
  implementation "androidx.navigation:navigation-ui:$nav_version"
  }

2 在res目录下创建导航配置文件

配置对应的Fragment和跳转action。
<?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" android:id="@+id/nav_graph"
    app:startDestination="@id/fragmentA">
    <!-- 这是全局action -->
    <action
        android:id="@+id/action_to_fragmentA"
        app:destination="@id/fragmentA" />
    <action
        android:id="@+id/action_to_fragmentB"
        app:destination="@id/fragmentB" />
    <action
        android:id="@+id/action_to_fragmentC"
        app:destination="@id/fragmentC" />
    <action
        android:id="@+id/action_to_fragmentD"
        app:destination="@id/fragmentD" />

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.example.navigation.FragmentA"
        android:label="FragmentA" >
    </fragment>
    <fragment
        android:id="@+id/fragmentB"
        android:name="com.example.navigation.FragmentB"
        android:label="FragmentB" >

    </fragment>
    <fragment
        android:id="@+id/fragmentC"
        android:name="com.example.navigation.FragmentC"
        android:label="FragmentC" >

    </fragment>
    <fragment
        android:id="@+id/fragmentD"
        android:name="com.example.navigation.FragmentD"
        android:label="FragmentD" />
</navigation>

3 在MainActivity的xml中添加标签

要使用NavHostFragment,**Activity必须继承 androidx.appcompat.app.AppCompatActivity/ androidx.fragment.app.Fragment;不然会报错的**。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <fragment
        android:id="@+id/nav_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"  // 使用Fragment回退栈
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

4 在Fragment中添加跳转逻辑

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    Log.i(TAG, "FragmentA onViewCreated: ");
    mBt = view.findViewById(R.id.button);
    mBt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // 进行导航
            Navigation.findNavController(view).navigate(R.id.action_fragmentA_to_fragmentB);
        }
    });
}
Navigation.findNavController(view).navigate(R.id.action_fragmentA_to_fragmentB),这里Fragment添加使用的ft.replace(mContainerId, frag);
FragmentNavigator.java

@Nullable
@Override
public NavDestination navigate(@NonNull Destination destination, @Nullable Bundle args,
        @Nullable NavOptions navOptions, @Nullable Navigator.Extras navigatorExtras) {
   .......
    String className = destination.getClassName();
    if (className.charAt(0) == '.') {
        className = mContext.getPackageName() + className;
    }
    final Fragment frag = instantiateFragment(mContext, mFragmentManager,
            className, args);
    frag.setArguments(args);
    .......

    ft.replace(mContainerId, frag);
    ft.setPrimaryNavigationFragment(frag);
    ........
    ft.setReorderingAllowed(true);
    ft.commit();

}

二 BottomNavigationView + Navigation 使用

1 Activity xml添加BottomNavigationView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <fragment
        android:id="@+id/nav_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bottom"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/menu_bottom"/>
</androidx.constraintlayout.widget.ConstraintLayout>

2 配置menu

**注意:item的id必须和navi_graph中Fragment的id一致/或者actionId一致。**
导航时,是通过actionId或者目标Fragmentid跳转的。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
<!--    使用目标FragmentId作为itemId-->
<!--    <item-->
<!--        android:id="@+id/fragmentA"-->
<!--        android:icon="@drawable/my"-->
<!--        android:title="我的" />-->
<!--    <item-->
<!--        android:id="@+id/fragmentB"-->
<!--        android:icon="@drawable/community"-->
<!--        android:title="社区"/>-->
<!--    <item-->
<!--        android:id="@+id/fragmentC"-->
<!--        android:icon="@drawable/chat"-->
<!--        android:title="聊天" />-->
<!--    <item-->
<!--        android:id="@+id/fragmentD"-->
<!--        android:icon="@drawable/setting"-->
<!--        android:title="设置" />-->

    <!--    使用actionId作为itemId-->
    <item
        android:id="@+id/action_to_fragmentA"
        android:icon="@drawable/my"
        android:title="我的"/>
    <item
        android:id="@+id/action_to_fragmentB"
        android:icon="@drawable/community"
        android:title="社区" />
    <item
        android:id="@+id/action_to_fragmentC"
        android:icon="@drawable/chat"
        android:title="聊天" />
    <item
        android:id="@+id/action_to_fragmentD"
        android:icon="@drawable/setting"
        android:title="设置" />
</menu>

3 在activity中关联navigation和BottomNavigationView

public class BottomActivity extends AppCompatActivity {
    private BottomNavigationView mBottomView;
    private NavController mNavController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom);
        mBottomView = findViewById(R.id.bottom);
        mNavController = Navigation.findNavController(this, R.id.nav_fragment);
        // 关联NavigationView和导航控制器
        NavigationUI.setupWithNavController(mBottomView, mNavController);
    }
}