1:添加依赖
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
2: 入口布局添加容器,声明默认 导航文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<TextView
android:id="@+id/tv_des"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/teal_200"
android:gravity="center"
android:layout_gravity="bottom"
android:text="导航学习"
/>
<!-- 碎片容器
使用 FragmentContainerView,不需要在编写代码 使用FragmentManager 开启事务 添加
name 路由导航碎片 执行处理
-->
<androidx.fragment.app.FragmentContainerView
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:id="@+id/containet"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_path" />
</FrameLayout>
3: res 下创建?navigation 目录,创建导航文件
导航文件需要一个启动目的地 startDestination
<?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/navi"
app:startDestination="@id/to_home">
<!-- id:用于导航指向
name:目的地(全包名路径)
layout:目的地布局
-->
<fragment
android:id="@+id/to_home"
android:name="cn.hualand.navstudy.fragment.Home"
android:label="@string/home"
tools:layout="@layout/fragment_home">
<!-- id:动作唯一id 用于指向
destination:目的地 id
enterAnim:进入动画
exitAnim:退出动画
-->
<action
android:id="@+id/home_to_setting"
app:destination="@id/to_setting"
app:enterAnim="@anim/open_enter"
app:exitAnim="@anim/close_enter" />
<action
android:id="@+id/home_to_about"
app:destination="@id/to_about"
app:enterAnim="@anim/open_enter"
app:exitAnim="@anim/close_enter" />
<action
android:id="@+id/home_to_await"
app:destination="@id/to_await"
app:enterAnim="@anim/open_enter"
app:exitAnim="@anim/close_enter" />
</fragment>
<fragment
android:id="@+id/to_await"
android:name="cn.hualand.navstudy.fragment.Await"
android:label="@string/home"
tools:layout="@layout/fragment_await">
<action
android:id="@+id/await_to_home"
app:destination="@id/to_home"
app:enterAnim="@anim/open_enter"
app:exitAnim="@anim/close_enter" />
<action
android:id="@+id/home_to_about"
app:destination="@id/to_about"
app:enterAnim="@anim/open_enter"
app:exitAnim="@anim/close_enter" />
</fragment>
<activity
android:id="@+id/to_setting"
android:name="cn.hualand.navstudy.SettingActivity"
android:label="@string/setting"
tools:layout="@layout/activity_setting" />
<activity
android:id="@+id/to_about"
android:name="cn.hualand.navstudy.AboutActivity"
android:label="@string/about"
tools:layout="@layout/activity_about" />
</navigation>
动画代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%"
android:duration="1000"
android:toXDelta="0%" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:duration="1000"
android:toXDelta="100%" />
</set>
4:碎片 活动相关代码 只贴几个代码,其他雷同
package cn.hualand.navstudy.fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import cn.hualand.navstudy.R
import kotlinx.android.synthetic.main.fragment_home.*
class Home:Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var view=inflater.inflate(R.layout.fragment_home,container,false)
return view
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
//设置点击监听
tv_setting.setOnClickListener {
//导航跳转 通过所属下的Action 指向
NavHostFragment.findNavController(this).navigate(R.id.home_to_setting)
}
tv_about.setOnClickListener {
NavHostFragment.findNavController(this).navigate(R.id.home_to_about)
}
tv_await.setOnClickListener {
NavHostFragment.findNavController(this).navigate(R.id.home_to_await)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_pic"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:gravity="center"
android:text="welcome to HomePage,use study navigation test"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/tv_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_description"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="@color/black"
android:gravity="center"
android:text="setting"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/tv_about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_setting"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@color/black"
android:gravity="center"
android:text="about"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/tv_await"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_about"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@color/black"
android:gravity="center"
android:text="await"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
package cn.hualand.navstudy.fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.NavHostFragment
import cn.hualand.navstudy.R
import kotlinx.android.synthetic.main.fragment_await.*
class Await: Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var view=inflater.inflate(R.layout.fragment_await,container,false)
return view
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
tv_home.setOnClickListener {
NavHostFragment.findNavController(this).navigate(R.id.await_to_home)
}
tv_about.setOnClickListener {
NavHostFragment.findNavController(this).navigate(R.id.home_to_about)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/design_default_color_error"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_pic"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="@mipmap/ic_launcher" />
<Button
android:id="@+id/tv_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_pic"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="@color/black"
android:gravity="center"
android:text="home"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/tv_about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_home"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="@color/black"
android:gravity="center"
android:text="about"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
|