java导包
dependencies {
//navigation
def nav_version = "2.3.5"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
}
kotlin导包
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
如果使用SafeArgs(gradle插件,数据传递时提供安全性)插件,需要添加以下内容
工程下build.gradle
buildscript {
repositories {
google()
jcenter()
}
ext.navigationVersion = "2.3.5"
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
src下添加
apply plugin: 'kotlin-android-extensions' //java 工程不需要
apply plugin: 'androidx.navigation.safeargs'
使用
1、创建界面fragment
public class Navigation1Fragment extends Fragment {
@BindView(R.id.btn1)
Button btn1;
@BindView(R.id.btn2)
Button btn2;
private Unbinder unbinder;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_navigation1, container,false);
unbinder = ButterKnife.bind(this,view);
return view;
}
@OnClick({R.id.btn1, R.id.btn2})
public void onClick(View view) {
NavController navController = Navigation.findNavController(getView());
switch (view.getId()) {
case R.id.btn1:
navController.navigate(R.id.navigation2Fragment);
break;
case R.id.btn2:
navController.navigate(R.id.navigation3Fragment);
break;
}
}
@Override
public void onDestroy() {
super.onDestroy();
unbinder.unbind();
}
}
public class Navigation2Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_navigation2, container,false);
return view;
}
}
public class Navigation3Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_navigation3, container,false);
return view;
}
}
2、创建res下创建navigation/navigationtest.xml
通过Design添加创建的fragment
<?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/navigationtest"
app:startDestination="@id/navigation1Fragment">
<fragment
android:id="@+id/navigation1Fragment"
android:name="com.zqq.h5shell.activity.jectpack_navigation.fragment.Navigation1Fragment"
android:label="Navigation1Fragment" />
<fragment
android:id="@+id/navigation2Fragment"
android:name="com.zqq.h5shell.activity.jectpack_navigation.fragment.Navigation2Fragment"
android:label="Navigation2Fragment" />
<fragment
android:id="@+id/navigation3Fragment"
android:name="com.zqq.h5shell.activity.jectpack_navigation.fragment.Navigation3Fragment"
android:label="Navigation3Fragment" />
</navigation>
3 创建activity
布局如下,FragmentContainView
主要属性:
android:name=“androidx.navigation.fragment.NavHostFragment”
app:defaultNavHost=“false” //是否关联返回键
app:navGraph="@navigation/navigationtest"
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="false"
app:layout_constraintBottom_toBottomOf="@id/navigationview"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigationtest" />
<com.google.android.material.navigation.NavigationView
app:layout_constraintTop_toBottomOf="@id/nav_host_fragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_weight="1"
android:id="@+id/navigationview"
app:menu="@menu/navigation"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_500"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity
@Route(path = ARouterContent.NavigationTestActivity)
public class NavigationTestActivity extends AppCompatActivity {
private NavigationView navigationView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
navigationView = findViewById(R.id.navigationview);
NavHostFragment navHostFragment = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavController navController = navHostFragment.getNavController();
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem menuItem) {
int itemId = menuItem.getItemId();
switch (itemId){
case R.id.navigation1Fragment:
navController.navigate(R.id.navigation1Fragment);
break;
case R.id.navigation2Fragment:
navController.navigate(R.id.navigation2Fragment);
break;
case R.id.navigation3Fragment:
navController.navigate(R.id.navigation3Fragment);
break;
}
return true;
}
});
}
}
获取navController 时不可使用 Navigation.findNavController(this,R.id.nav_host_fragment);
问题原因就是在onCreate生命周期里FragmentContainerView对应的NavController未构建成功
效果如下
与BottomNavigationView一起使用
Activity布局
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentcontainerview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/bottom_navigation"
app:defaultNavHost="false"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottomnavigationview"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomnavigationview"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_45"
app:layout_constraintTop_toBottomOf="@id/fragmentcontainerview"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_weight="1"
app:menu="@menu/navigation_bottom"
app:itemTextColor="@color/bootom_selector"
app:itemIconTint="@color/bootom_selector"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity
@Route(path = ARouterContent.BottomNavigationTestActivity)
public class BottomNavigationTestActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation);
NavHostFragment navHostFragment = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentcontainerview);
NavController navController = navHostFragment.getNavController();
BottomNavigationView bottomnavigationview=findViewById(R.id.bottomnavigationview);
bottomnavigationview.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.mainFragment:
Log.i("zqqq","mainFragment");
navController.navigate(R.id.mainFragment);
break;
case R.id.demoFragment:
Log.i("zqqq","demoFragment");
navController.navigate(R.id.demoFragment);
break;
case R.id.meFragment:
Log.i("zqqq","meFragment");
navController.navigate(R.id.meFragment);
break;
}
return true;
}
});
bottomnavigationview.setSelectedItemId(R.id.mainFragment);
}
}
navigation
<?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/bottom_navigation.xml"
app:startDestination="@id/demoFragment">
<fragment
android:id="@+id/demoFragment"
android:name="com.zqq.h5shell.activity.jectpack_navigation.bottomfragment.DemoFragment"
android:label="DemoFragment" />
<fragment
android:id="@+id/mainFragment"
android:name="com.zqq.h5shell.activity.jectpack_navigation.bottomfragment.MainFragment"
android:label="MainFragment" />
<fragment
android:id="@+id/meFragment"
android:name="com.zqq.h5shell.activity.jectpack_navigation.bottomfragment.MeFragment"
android:label="MeFragment" />
</navigation>
效果: