728x90
class ViewModel(application: Application) : AndroidViewModel(application) {
var num = MutableLiveData<Int>()
init {
num.value = 0
}
fun addNum(){
num.value = num.value?.plus(1)
}
fun subtractNum(){
num.value = num.value?.minus(1)
}
}
1. Gradle 세팅
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
}
android {
...
buildFeatures{
dataBinding true
}
...
}
dependencies {
...
// LifeCycle
def lifecycle_version = "2.2.0"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.activity:activity-ktx:1.1.0"
}
2. ViewModel 먼저 만들어줍니다.
class ViewModel(application: Application) : AndroidViewModel(application) {
// 표출할 숫자
var num = MutableLiveData<Int>()
// 초기화
init {
num.value = 0
}
// +1 씩 더하기
fun addNum(){
num.value = num.value?.plus(1)
}
// -1 씩 빼기
fun subtractNum(){
num.value = num.value?.minus(1)
}
}
3. 레이아웃 및 바인딩
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!---A--------------------------------------------------->
<data>
<variable
name="viewModel"
type="com.example.kotlinexample.ViewModel" />
</data>
<!-------------------------------------------------------->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{Integer.toString(viewModel.num)}"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.37" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="296dp"
android:text="+"
android:onClick="@{()->viewModel.addNum()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btn_subtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="248dp"
android:text="-"
android:onClick="@{()->viewModel.subtractNum()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
A 부분의 데이터 태그를 명시해줍니다.
4. View 와 ViewModel 연동 및 lifecycleOwner 생명주기 관리
class MainActivity : AppCompatActivity() {
private lateinit var mBinding: ActivityMainBinding
private val mModel : ViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mBinding = DataBindingUtil.setContentView(this,R.layout.activity_main)
mBinding.lifecycleOwner = this
mBinding.viewModel = mModel
}
}
'안드로이드 코틀린' 카테고리의 다른 글
[Android][Kotlin]BackPressed시, 중첩된 Fragment 순서대로 종료하기 (0) | 2022.04.23 |
---|---|
[Kotlin][Android] 삼각형 그리기 (OpenGL) (0) | 2022.04.16 |
[Android][kotlin] EditText Submit처럼 사용하기 (0) | 2022.04.06 |
[Kotlin][Android] 안드로이드 OCR 기능 만들기 (0) | 2022.04.01 |
[Kotlin][Android] 키보드 보이기/숨기기 (0) | 2022.03.20 |