赞
踩
开发环境
教程
深入详解 Jetpack Compose | 实现原理
实际开发体验和flutter很像,但是实际原理好像不同,flutter中组件都是widget对象,比较容易理解
而compose中组件并没有返回值,编译器会对@Composable
的方法进行处理,具体原理需要深入了解
@Composable
官方说明关于@Composable
,有几点值得注意:
dependencies { def lifecycle_version = "2.3.1" def compose_version = '1.0.0-beta01' // ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" // LiveData implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" // Jetpack Compose Integration for ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha04" implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-alpha03" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.material:material:$compose_version" implementation "androidx.compose.ui:ui-tooling:$compose_version" implementation "androidx.compose.runtime:runtime-livedata:$compose_version" implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0-rc01' implementation 'androidx.activity:activity-compose:1.3.0-alpha02' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" }
package com.example.composeapplication import android.os.Bundle import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.runtime.livedata.observeAsState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.constraintlayout.compose.ConstraintLayout import androidx.lifecycle.viewmodel.compose.viewModel import com.example.composeapplication.ui.theme.ComposeApplicationTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { ComposeApplicationTheme { // 学过flutter的应该很熟悉,基本的 Material 布局 Scaffold( topBar = { TopAppBar(title = { Text(text = "计数器") }) }, ) { ConstraintLayoutContent() } } } } } @Composable fun ConstraintLayoutContent() { // 获取viewModel val viewModel: MainViewModel = viewModel() ConstraintLayout( modifier = Modifier.fillMaxSize(), //充满父组件 ) { val (fab, text) = createRefs() val count = viewModel.count.observeAsState() Text( text = count.value.toString(), modifier = Modifier.constrainAs(text) { centerTo(parent) }, fontSize = 20.sp, ) FloatingActionButton( modifier = Modifier.constrainAs(fab) { bottom.linkTo(parent.bottom, margin = 16.dp) end.linkTo(parent.end, margin = 16.dp) }, onClick = { viewModel.add() }, ) { Icon( painter = painterResource(id = R.drawable.ic_baseline_add_24), contentDescription = "add", tint = Color.White ) } } }
package com.example.composeapplication
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class MainViewModel : ViewModel() {
private val _count = MutableLiveData(0)
val count: LiveData<Int> = _count
fun add() {
_count.value = _count.value?.plus(1)
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。