MainLauncher GardentActivity
使用DataBinding,参考:DataBinding官方文档,DataBind浅析
基本配置
1.DataBinding
-
配置build.gradle
AndroidStudio3.2+模块build.gradle,添加
dataBinding {
enabled = true
}android { compileSdkVersion 28 defaultConfig { applicationId "com.tools.testmvvm2" minSdkVersion 19 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } dataBinding { enabled = true } }
-
使用androidx,
在AndroidStudio3.2中有插件,能直接将支持库全部转换为androidx,Refactor ->Migrate to AndroidX
其效果将原来的v4,v7支持包改为更强大的Androidx包,手动修改需要修改gradle及各类中的引用
-
布局配置
区别于以前的布局,顶层布局使用layout,有数据的话会添加相应数据,没有的话不能生成对相应的Binding
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<layout>
2.Navigation
- 开启视图管理器
-
添加依赖
ext.navigationVersion='1.0.0-alpha06' implementation "android.arch.navigation:navigation-fragment-ktx:$rootProject.navigationVersion" implementation "android.arch.navigation:navigation-ui-ktx:$rootProject.navigationVersion"
-
创建navigation视图管理器
在res文件夹上点击右键->NEW->Android resource file->输入文件名称并选择resource type为Navigation->确认
-
添加HostFragment,还需要创建一个主Fragment
最初要有个起始页面,叫
start destination
,处于栈底,是启动时的第一个页面,当然也是返回可见的最后一个页面。多个 destination 连接起来就组成了一个导航图
,类似于一种栈结构,页面先进先出。destination 之间的连接叫做action
。
作者:七适散人
链接:http://www.imooc.com/article/74402
来源:慕课网<fragment android:id="@+id/garden_nav_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/garden_nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</LinearLayout>
The Navigation Architecture Component includes a
NavigationUI
class. This class has several static methods you can use connect menu items with navigation destinations. For example, the following code shows how to use thesetupWithNavController()
method to connect items in the menu drawer to theNavigationView
:导航架构组件包括一个
NavigationUI
类。此类有几个静态方法,您可以使用带有导航目标的连接菜单项。例如,以下代码显示如何使用该setupWithNavController()
方法将菜单抽屉中的项目连接到NavigationView
:
-
菜单抽屉中的点击跳转
1.GardenActivity的xml布局,NavigationView中的menu,res->menu->menu_navigation.xml
2.res->navigation->nav_garden.xml
使1中item的id,和2中fragment的id相同便能够跳转
menu_navigation.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/garden_fragment" android:title="@string/my_garden_title"/> <item android:id="@+id/plant_list_fragment" android:title="@string/plant_list_title"/> </menu>
nav_garden.xml
<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/garden_fragment"> <fragment android:id="@+id/garden_fragment" android:name="com.google.samples.apps.sunflower.GardenFragment" android:label="@string/my_garden_title" tools:layout="@layout/fragment_garden" /> <fragment android:id="@+id/plant_list_fragment" android:name="com.google.samples.apps.sunflower.PlantListFragment" android:label="@string/plant_list_title" tools:layout="@layout/fragment_plant_list"> <action android:id="@+id/action_plant_list_fragment_to_plant_detail_fragment" app:destination="@id/plant_detail_fragment" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" /> </fragment> <fragment android:id="@+id/plant_detail_fragment" android:name="com.google.samples.apps.sunflower.PlantDetailFragment" android:label="@string/plant_details_title" tools:layout="@layout/fragment_plant_detail"> <argument android:name="plantId" app:argType="string" /> </fragment> </navigation>
代码
-
onCreate
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding: ActivityGardenBinding = DataBindingUtil.setContentView(this, R.layout.activity_garden) drawerLayout = binding.drawerLayout val navController = Navigation.findNavController(this, R.id.garden_nav_fragment) // Set up ActionBar setSupportActionBar(binding.toolbar) NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout) // Set up navigation menu binding.navigationView.setupWithNavController(navController) }
这样GardenActivity便分析完了,很多功能都是新鲜的,文章也比较少,想要在深入了解具体实现,如为什么不用setClickerListener了,还需要源码的研究