看到这些博客的时候,相信小伙伴们已经看了不少的有关于Android Jetpack的知识了,关于Jetpack的原理,有些文章讲的很好,这里就不重复了,此系列的博客,旨在通过一些很简单的小demo,和大家一起熟悉最基本的用法~
Android Jetpack -- ViewModel & LiveData
Android Jetpack -- paging & room
项目git地址:https://github.com/zhangtiansimple/jetpack_demo
------------------------------------------------------------------------------------------------------------------------
Navigation的引入在一定程度上优化了传统的对于Fragment的控制,接下来看看Navigation的使用。
首先建立三个Fragment和它们的布局文件
class Fragment1 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment1, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn.setOnClickListener { Navigation.findNavController(it).navigate(R.id.action_page2) }
}
}
class Fragment2 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment2, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_return.setOnClickListener { Navigation.findNavController(it).navigateUp() }
btn_go.setOnClickListener { Navigation.findNavController(it).navigate(R.id.action_page3) }
}
}
class Fragment3 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment3, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//点击返回page2
btn.setOnClickListener { Navigation.findNavController(it).navigateUp() }
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/btn"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="current page1, click to go page2"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/btn_return"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:text="current page2, click to return page1"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/btn_go"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:text="current page2, click to go page3"
android:layout_below="@+id/btn_return"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/btn"
android:layout_centerInParent="true"
android:text="current page3, click to return page2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
接着在res资源目录下建立navigation路径,并且建立nav_graph.xml文件,xml文件里配置了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"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@id/page1Fragment"
tools:ignore="UnusedNavigation">
<fragment
android:id="@+id/page1Fragment"
android:name="com.study.jetpacktest.navigation.Fragment1"
android:label="fragment_page1"
tools:layout="@layout/fragment1">
<action
android:id="@+id/action_page2"
app:destination="@id/page2Fragment"/>
</fragment>
<fragment
android:id="@+id/page2Fragment"
android:name="com.study.jetpacktest.navigation.Fragment2"
android:label="fragment_page2"
tools:layout="@layout/fragment2">
<action
android:id="@+id/action_page1"
app:popUpTo="@id/page1Fragment"/>
<action
android:id="@+id/action_page3"
app:destination="@id/nav_graph_page3"/>
</fragment>
<navigation
android:id="@+id/nav_graph_page3"
app:startDestination="@id/page3Fragment">
<fragment
android:id="@+id/page3Fragment"
android:name="com.study.jetpacktest.navigation.Fragment3"
android:label="fragment_page3"
tools:layout="@layout/fragment3"/>
</navigation>
</navigation>
最后是Activity,在Activity的布局文件里引入一个fragment作为根容器
<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
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"/>
</LinearLayout>
class NavigationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navigation)
}
override fun onSupportNavigateUp() =
findNavController(this, R.id.my_nav_host_fragment).navigateUp()
}
一个简单的Navigation demo就写好了