티스토리 뷰

android

android mvvm 예제

노명규 2023. 1. 30. 10:17

본 예제에 model 부분은 없습니다.

data 관련 핸들링은 모두 model에서 해야하고, viewmodel에선 view에 로직만 담당합니다. 

 

databinding, kotlin을 사용합니다. 

 

사용 파일들은 아래와 같습니다.

* AActivity.kt
* A1Viewmodel.kt

* A2Viewmodel.kt
* a_activity.xml
* a1_viewmodel.xml
* a2_viewmodel.xml

 

 

1. AActivity.kt

class AActivity :AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        DataBindingUtil.setContentView<AActivityBinding>(this, R.layout.a_activity).apply {
            lifecycleOwner = this@AActivity
            a1ViewModel = ViewModelProvider(this@AActivity).get(A1ViewModel::class.java);
            a2ViewModel = ViewModelProvider(this@AActivity).get(A2ViewModel::class.java);
        }
    }
}

 

 

 

2. A1ViewModel.kt

class A1ViewModel(val app: Application) : AndroidViewModel(app) {
    init {
        Toast.makeText(app,"A1ViewModel ", Toast.LENGTH_LONG).show()
    }
}

 

3. A2ViewModel.kt

class A2ViewModel(val app: Application) : AndroidViewModel(app) {
    init {
        Toast.makeText(app,"A2ViewModel", Toast.LENGTH_LONG).show()
    }
}

 

4. a_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="a1ViewModel"
            type="com.example.A1ViewModel" />
        <variable
            name="a2ViewModel"
            type="com.example.A2ViewModel" />
    </data>

    <LinearLayout
        xmlns:bind="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/a1"
            layout="@layout/a1_viewmodel"
            bind:a1vm="@{a1ViewModel}" />

        <include
            android:id="@+id/a2"
            layout="@layout/a2_viewmodel"
            bind:a2vm="@{a2ViewModel}" />

    </LinearLayout>

</layout>

 

5. a1_viewmodel.xml

<?xml version="1.0" encoding="utf-8"?>

<layout>
    <data>
        <variable
            name="a1vm"
            type="com.example.A1ViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="a1_vm"
            tools:ignore="MissingConstraints" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

 

 

6. a2_viewmodel.xml

<?xml version="1.0" encoding="utf-8"?>

<layout>
    <data>
        <variable
            name="a2vm"
            type="com.example.A2ViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="a2_vm"
            tools:ignore="MissingConstraints" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

 

 

 

// onCreate에서 항상 vm을 초기화해버리면, ui에 state가 바뀌면 vm의 데이터가 날라가지 않을까?

그럴 경우에

class AActivity :AppCompatActivity() {
    private lateinit var binding: AActivityBinding

    private val a1ViewModel: A1ViewModel by viewModels()
    private val a2ViewModel: A2ViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView<AActivityBinding>(this, R.layout.a_activity).apply {
            lifecycleOwner = this@AActivity
            aViewModel = ViewModelProvider(this@AActivity).get(AViewModel::class.java);
            a1ViewModel = this@AActivity.a1ViewModel
            a2ViewModel = this@AActivity.a2ViewModel
        }
    }
}

로 사용하면 될 것 같음, 

'android' 카테고리의 다른 글

system screen rotate click listener  (0) 2023.03.07
retrofit으로, rx and coroutine  (0) 2023.02.03
retrofit  (0) 2022.09.21
by viewmodels() 쓰는법,  (0) 2022.08.18
android Frame Animation, Animation Drawable 사용 방법  (0) 2020.04.27