티스토리 뷰
본 예제에 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 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- https://hwan-shell.tistory.com/244
- not starting .bash_profile
- 명령어
- 구글 맵 선그리기
- 차번호 정규표현식
- 클래스형 코드
- insert
- ubunut android
- not found adb
- not working adb
- https://medium.com/@limgyumin/%EC%BD%94%ED%8B%80%EB%A6%B0-%EC%9D%98-apply-with-let-also-run-%EC%9D%80-%EC%96%B8%EC%A0%9C-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94%EA%B0%80-4a517292df29
- Android
- spannableText
- adb 환경변수
- 차번호 정규식
- 귀찮아;;
- React Native
- 함수형 코드
- 자바
- MongoDB
- text 부분 색 칠하기
- 데이터베이스
- 구글 맵 경로 그리기
- 안드로이드
- react native state
- mac android
- rn
- ubunut 설치 link
- nosql
- mongo db
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함