注册

Jetpack之Navigation(1)

1.基本使用

引入库



def nav_version = "2.3.2"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
复制代码

Activity布局


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<!--
app:defaultNavHost="true"
拦截系统back键
-->

<androidx.fragment.app.FragmentContainerView
android:id="@+id/my_nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph_main"/>


</LinearLayout>
复制代码

在res/navigation目录下nav_graph_main.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"
android:id="@+id/nav_graph_main.xml"
app:startDestination="@id/page1Fragment">

<fragment
android:id="@+id/page1Fragment"
android:name="com.example.lsn4_navigationdemo.MainPage1Fragment"
android:label="fragment_page1"
tools:layout="@layout/fragment_main_page1">


<!--action:程序中使用id跳到destination对应的类-->
<action
android:id="@+id/action_page2"
app:destination="@id/page2Fragment" />

</fragment>

<fragment
android:id="@+id/page2Fragment"
android:name="com.example.lsn4_navigationdemo.MainPage2Fragment"
android:label="fragment_page2"
tools:layout="@layout/fragment_main_page2">

<action
android:id="@+id/action_page1"
app:destination="@id/page1Fragment" />

<action
android:id="@+id/action_page3"
app:destination="@id/page3Fragment" />

</fragment>

<!-- <navigation-->
<!-- android:id="@+id/nav_graph_page3"-->
<!-- app:startDestination="@id/page3Fragment">-->
<fragment
android:id="@+id/page3Fragment"
android:name="com.example.lsn4_navigationdemo.MainPage3Fragment"
android:label="fragment_page3"
tools:layout="@layout/fragment_main_page3"
>

<action
android:id="@+id/action_page2"
app:destination="@id/page2Fragment"/>

</fragment>


</navigation>
复制代码

Fragment中调用跳转


//方式一:直接跳入指定的fragment
Navigation.findNavController(view).navigate(R.id.page2Fragment);

//方式二:通过action
Navigation.findNavController(view).navigate(R.id.action_page2); //跳入page2
复制代码

Activity中调用跳转


//获取controller方式一
NavController controller=Navigation.findNavController(this,R.id.my_nav_host_fragment);

//获取controller方式二
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.my_nav_host_fragment);
NavController controller = navHostFragment.getNavController();

//跳转
controller.navigate(R.id.page2Fragment);
复制代码

Activity绑定navigation


除了在Activity的布局中指定navigation布局资源以外,还可以通过java代码进行设置


public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//方式一
val finalHost = NavHostFragment.create(R.navigation.nav_graph_main)
supportFragmentManager.beginTransaction()
.replace(R.id.ll_fragment_navigation, finalHost)
.setPrimaryNavigationFragment(finalHost)
.commit();
}

//方式二
@Override
public boolean onSupportNavigateUp() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.my_nav_host_fragment);
return NavHostFragment.findNavController(fragment).navigateUp();
}
}
复制代码

底部导航


引入底部导航控件


implementation 'com.google.android.material:material:1.1.0'
复制代码

布局


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<!--
app:defaultNavHost="true"
拦截系统back键
-->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/my_nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph_main"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
app:itemTextColor="#ff0000"
app:menu="@menu/menu"/>

</LinearLayout>
复制代码

menu


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/page1Fragment"
android:icon="@drawable/ic_launcher_foreground"
android:title="1"/>
<item
android:id="@+id/page2Fragment"
android:icon="@drawable/ic_launcher_foreground"
android:title="2"/>
<item
android:id="@+id/page3Fragment"
android:icon="@drawable/ic_launcher_foreground"
android:title="3"/>

</menu>
复制代码

Activity绑定导航


public class MainActivity extends AppCompatActivity {

BottomNavigationView bottomNavigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.action_page1);
bottomNavigationView=findViewById(R.id.nav_view);

//获取controller方式一
//NavController controller=Navigation.findNavController(this,R.id.my_nav_host_fragment);

//获取controller方式二
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.my_nav_host_fragment);
NavController controller = navHostFragment.getNavController();

//绑定导航
NavigationUI.setupWithNavController(bottomNavigationView,controller);

}

作者:贾里
链接:https://juejin.cn/post/6953624951194664968
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
">

1 个评论

写得好

要回复文章请先登录注册