前提:公司要两周写一个demo,不用上线的那种,只有一些简单的交互,连一个接口都木有,嗯嗯,很开心地开始我的试用新技术之路。听说最近jetpack很火,好,就是你了,出来吧,皮卡丘,具体好在哪里,请看官网介绍
然而看着官网第一步就难住我了,同一个世界不同的studio?你们所说的Activity & Fragment + ViewModel 模板在哪里?我的studio有毒?
好好好,算你们狠,还好有demo可以下载。
项目最初包含三个类:StartActivity、StartFragment 和 StartViewModel。
StartViewModel对象:
class StartViewModel : ViewModel() {private val _data = MutableLiveData<String>()
val data
: LiveData<String>get() =_data
init
{
_data
.value = "Hello, Jetpack!"}}
StartFragment 对象:
class StartFragment : Fragment() {
private lateinit var viewModel: StartViewModel
companion object {
fun newInstance() = StartFragment()
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_start, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
// Obtain ViewModel from ViewModelProviders, using this fragment as LifecycleOwner.
viewModel = ViewModelProviders.of(this).get(StartViewModel::class.java)
// Observe data on the ViewModel, exposed as a LiveData
viewModel.data.observe(this, Observer { data->
// Set the text exposed by the LiveData
view?.findViewById(R.id.text)?.text = data
})
}
}
fragment_start布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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=".StartFragment">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
tools:text="Text from VM here"
app:layout_constraintBottom_toTopOf="@+id/navigate_bt"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.79" />
<Button
android:id="@+id/navigate_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="@string/navigate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text" />
</android.support.constraint.ConstraintLayout>
StartActivity 对象:
class StartActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_start)
}
}
activity_start布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
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:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</android.support.v4.widget.DrawerLayout>
可以用了吗?当然不可以...MutableLiveData/LiveData哪里来的?ViewModel()哪里来的?
app下build.gradle文件增加depencies:
dependencies {
// See versions.gradle file
implementation fileTree(dir:'libs',include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$versions.kotlin"
implementation"com.android.support:appcompat-v7:$versions.support"
implementation"com.android.support.constraint:constraint-layout:$versions.constraint_layout"
implementation"com.android.support:appcompat-v7:$versions.support"
implementation"com.android.support:design:$versions.support"
// LiveData + ViewModel
implementation"android.arch.lifecycle:extensions:$versions.lifecycle"
}
项目目录下的build.gradle增加dependencies:
dependencies {
classpath'com.android.tools.build:gradle:3.2.0-alpha13'
classpath'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha01'
classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
我们注意到在activity_start布局文件中有两句很奇怪的东西:
android:name="androidx.navigation.fragment.NavHostFragment"
和
app:navGraph="@navigation/nav_graph
这里明明应该是startFrament的,为啥会是这个鬼,这个鬼啥啥玩意儿?瞅瞅同站的导航卡Navigation 小姐,不想瞅的直接按照下图新建:
nav_graph配置:
<?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"
app:startDestination="@+id/launcher_home">
<fragment android:id="@+id/launcher_home"
android:label="Home"
android:name="com.android.samples.arch.componentsbasicsample.StartFragment" >
<action android:id="@+id/end_action" app:destination="@id/end_dest"/>
</fragment>
</navigation>
好,跑起来吧,恭喜你,完成第一个jetpack程序,hello world!
附件:demo下载