第一行Android Jetpack

前提:公司要两周写一个demo,不用上线的那种,只有一些简单的交互,连一个接口都木有,嗯嗯,很开心地开始我的试用新技术之路。听说最近jetpack很火,好,就是你了,出来吧,皮卡丘,具体好在哪里,请看官网介绍

然而看着官网第一步就难住我了,同一个世界不同的studio?你们所说的Activity & Fragment + ViewModel 模板在哪里?我的studio有毒?

Activity & Fragment + ViewModel 模板

好好好,算你们狠,还好有demo可以下载。

项目最初包含三个类:StartActivity、StartFragment 和 StartViewModel。

StartViewModel对象:


class StartViewModel : ViewModel() {private val _data = MutableLiveData()

val data

: LiveDataget() =_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布局文件:






StartActivity 对象:


class StartActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_start)

}

}

activity_start布局文件:





    



可以用了吗?当然不可以...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 小姐,不想瞅的直接按照下图新建:

Navigation导航配置文件

nav_graph配置:






    

        





好,跑起来吧,恭喜你,完成第一个jetpack程序,hello world!
附件:demo下载

你可能感兴趣的:(第一行Android Jetpack)