Jetpack –(四)DataBinding
DataBinding的意义
让布局文件承担了部分原本属于页面的工作,使页面布局耦合度进一步降低
文章作者: Carl Su
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 码行!
相关推荐
2022-08-05
Jetpack架构组件
Jetpack架构组件官方应用架构 DataStore1.类型 DataStore 提供两种不同的实现:Preferences DataStore 和 Proto DataStore。 Preferences DataStore 使用键存储和访问数据。此实现不需要预定义的架构,也不确保类型安全。 Proto DataStore 将数据作为自定义数据类型的实例进行存储。此实现要求您使用协议缓冲区来定义架构,但可以确保类型安全。 2.Gradle配置DataStore Typed // Typed DataStore (Typed API surface, such as Proto) dependencies { implementation("androidx.datastore:datastore:1.0.0") // optional - RxJava2 support ...
2022-08-05
LiveData transformations
Transformations 有两个: [Transformations.map](https://developer.android.com/reference/androidx/lifecycle/Transformations.html#map(androidx.lifecycle.LiveData, androidx.arch.core.util.Function)) [Transformations.switchMap](https://developer.android.com/reference/androidx/lifecycle/Transformations.html#switchMap(androidx.lifecycle.LiveData, androidx.arch.core.util.Function>))
2022-08-05
Data binding with ViewModel and LiveData
官方Demohttps://developer.android.com/codelabs/kotlin-android-training-live-data-data-binding#0 本文基于 ViewModel + LiveData 改造 Gradle配置android { buildFeatures { dataBinding true }} Add ViewModel data binding<layout ...> <data> <variable name="scoreViewModel" type="com.example.android.guesstheword.screens.score.ScoreViewModel" /> </data> ...

2022-08-05
LiveData和LiveData观察者
1.Add LiveData to ViewModel Add LiveData to ViewModel and encapsulate the LiveData // The current wordprivate val _word = MutableLiveData<String>()val word: LiveData<String> get() = _word// The current scoreprivate val _score = MutableLiveData<Int>()val score: LiveData<Int> get() = _score 2.Attach observers to the LiveData objects在Activity中使用: viewModel.score.observe(this, Observer { newScore -> Log.e(TAG, "$newScore" ) ...
2021-08-05
ViewModel和ViewModelProvider
依赖//ViewModelimplementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0' 创建ViewModelclass GameViewModel : ViewModel() { init { Log.i("GameViewModel", "GameViewModel created!") }} 关联并初始化声明private lateinit var viewModel: GameViewModel 初始化viewModel = ViewModelProvider(this).get(GameViewModel::class.java) Important: Always use ViewModelProvider to create ViewModel objects rather than directly instantiating an instance of...
2021-08-05
Android Navigation错误
开发环境:Android Studio: 2020.3.1 Patch 1gradle: 7.0.1navigation 版本: 2.3.5 eg1: 错误详情 API 'BaseVariant.getApplicationIdTextResource' is obsolete and has been replaced with 'VariantProperties.applicationId'.It will be removed in version 7.0 of the Android Gradle plugin.e: /Users/XXX/workspace/Android/demo/NavigationKotlin/app/build/generated/source/navigation-args/debug/com/txh/samples/apps/navigation/LoginFragmentDirections.kt: (12, 16): Class...
评论
ValineDisqus