Android
mvvm tutorial
aucd29
2018. 6. 27. 14:15
일단 공식 사이즈 정독 필요
- https://developer.android.com/topic/libraries/data-binding/?authuser=0&hl=ko
LiveData 와 ViewModel 에 대해서 친절하게 설명 되어 있음
- part1 : https://proandroiddev.com/mvvm-architecture-viewmodel-and-livedata-part-1-604f50cda1
- part2 : https://proandroiddev.com/mvvm-architecture-viewmodel-and-livedata-part-2-di-1a6b1f96d84b
Android Architecture Components #1 Guide to App Architecture (한글)
- http://tourspace.tistory.com/23
ViewModel VS AndroidViewModel
- https://stackoverflow.com/questions/44148966/androidviewmodel-vs-viewmodel
- Context 가 필요한 경우는 AndroidViewModel 을 쓰시라
Android databinding
- http://blog.btskyrise.com/posts/android-databinding
- https://github.com/pblachut/AndroidDatabinding
- https://github.com/Belozerow/android-mvvm
TextView 의 경우 ObservableInt 로 string id 를 할당해주면 됨 String 을 할당하려면 ObservableField<String> 이용
Spinner 는 링크를 참조
- https://qiita.com/Capotasto/items/3d636e1db3d92f0b852a
DialogFragment 의 dismiss 처리는 Callback Interface 또는 MutableLiveData<Boolean> 을 이용
- 코드 예 https://github.com/aucd29/common/blob/merge_2.0.6/library/src/main/java/net/sarangnamu/common/viewmodel/DialogViewModel.kt
RecyclerView 는 Adapter 와 ViewHolder 에 대해 기본 구조를 만들었기 때문에 이를 이용하면 됨
- https://gist.github.com/aucd29/0483cbd44d1a605890ead1d71a359e14
- HspAdapter<DATATYPE> 으로 일반적인 리스트들을 대응 가능
- 별도로 adapter / ViewHolder 를 생성할 필요 없음
- adapter 의 설정은 https://gist.github.com/aucd29/373b922400d8069cb608b6375ce34041 를 참조
ImageView 사용
- android:src 에 리소스 아이디 적용이 되지 않으므로 직접 바인딩 해줘야 한다.
- https://stackoverflow.com/questions/35809290/set-drawable-resource-id-in-androidsrc-for-imageview-using-data-binding-in-andr
- https://gist.github.com/aucd29/2d0fdcc2996314fef9a29c46db7fbe3d
XML 이용
- map 형태를 이용하려면 android:text='@{contact.phoneNumbers.get(0).get("mobile")}' 이런 식
- string.format 을 이용하려면 @{@string/id_name(model.value)} 이런 식
- ArrayList 를 참조시 <variable name="list" type="java.util.ArrayList & lt; java.lang.String & gt;"/> 이런 식
- import 해서 사용해도 무방하긴 하던데 알 수없는 이유로 워닝 뜰 수 있음 -_ -;
- View 에 visibility 를 설정할땐 ObservableInt 로 설정하고 View.GONE / View.VISIBLE 형태로 사용하면 됨
BindingAdapter 사용법
- 가령 LinearLayout 에 data list 를 add 하는 형태가 존재한다고 가정 한다면
- xml 에 다음과 같이 기입한다. 이때 기입한 이름은 사용자가 임의로 정하는 형태이다.
- <LinearLayout app:bindList="@{list}" ... />
- ViewModel 에는 static 형태로 method 를 생성하고 어노테이션을 추가한다 @BindingAdapter("bindList")
- https://gist.github.com/aucd29/06f28df979cde7fdff3f9db2f32f43bd
- xml 내의 view 끼리도 참조시 참조할 아이디가 @+id/hello_world 라면 @{helloWorld} 식으로 참조 가능 (아이디를 카멜 형태로)
ViewPagerBindingAdapter
- https://gist.github.com/aucd29/28e7c50e9b9706f79525e828bd8e9c87
implementing MVVM using LiveData, RxJava, Dagger Android
- https://proandroiddev.com/mvvm-architecture-using-livedata-rxjava-and-new-dagger-android-injection-639837b1eb6c
Building Offline-First App using MVVM, RxJava, Room and Priority Job Queue
- https://proandroiddev.com/offline-apps-its-easier-than-you-think-9ff97701a73f
- https://developer.android.com/topic/libraries/data-binding/?authuser=0&hl=ko
LiveData 와 ViewModel 에 대해서 친절하게 설명 되어 있음
- part1 : https://proandroiddev.com/mvvm-architecture-viewmodel-and-livedata-part-1-604f50cda1
- part2 : https://proandroiddev.com/mvvm-architecture-viewmodel-and-livedata-part-2-di-1a6b1f96d84b
Android Architecture Components #1 Guide to App Architecture (한글)
- http://tourspace.tistory.com/23
ViewModel VS AndroidViewModel
- https://stackoverflow.com/questions/44148966/androidviewmodel-vs-viewmodel
- Context 가 필요한 경우는 AndroidViewModel 을 쓰시라
Android databinding
- http://blog.btskyrise.com/posts/android-databinding
- https://github.com/pblachut/AndroidDatabinding
- https://github.com/Belozerow/android-mvvm
TextView 의 경우 ObservableInt 로 string id 를 할당해주면 됨 String 을 할당하려면 ObservableField<String> 이용
Spinner 는 링크를 참조
- https://qiita.com/Capotasto/items/3d636e1db3d92f0b852a
DialogFragment 의 dismiss 처리는 Callback Interface 또는 MutableLiveData<Boolean> 을 이용
- 코드 예 https://github.com/aucd29/common/blob/merge_2.0.6/library/src/main/java/net/sarangnamu/common/viewmodel/DialogViewModel.kt
RecyclerView 는 Adapter 와 ViewHolder 에 대해 기본 구조를 만들었기 때문에 이를 이용하면 됨
- https://gist.github.com/aucd29/0483cbd44d1a605890ead1d71a359e14
- HspAdapter<DATATYPE> 으로 일반적인 리스트들을 대응 가능
- 별도로 adapter / ViewHolder 를 생성할 필요 없음
- adapter 의 설정은 https://gist.github.com/aucd29/373b922400d8069cb608b6375ce34041 를 참조
ImageView 사용
- android:src 에 리소스 아이디 적용이 되지 않으므로 직접 바인딩 해줘야 한다.
- https://stackoverflow.com/questions/35809290/set-drawable-resource-id-in-androidsrc-for-imageview-using-data-binding-in-andr
- https://gist.github.com/aucd29/2d0fdcc2996314fef9a29c46db7fbe3d
XML 이용
- map 형태를 이용하려면 android:text='@{contact.phoneNumbers.get(0).get("mobile")}' 이런 식
- string.format 을 이용하려면 @{@string/id_name(model.value)} 이런 식
- ArrayList 를 참조시 <variable name="list" type="java.util.ArrayList & lt; java.lang.String & gt;"/> 이런 식
- import 해서 사용해도 무방하긴 하던데 알 수없는 이유로 워닝 뜰 수 있음 -_ -;
- View 에 visibility 를 설정할땐 ObservableInt 로 설정하고 View.GONE / View.VISIBLE 형태로 사용하면 됨
BindingAdapter 사용법
- 가령 LinearLayout 에 data list 를 add 하는 형태가 존재한다고 가정 한다면
- xml 에 다음과 같이 기입한다. 이때 기입한 이름은 사용자가 임의로 정하는 형태이다.
- <LinearLayout app:bindList="@{list}" ... />
- ViewModel 에는 static 형태로 method 를 생성하고 어노테이션을 추가한다 @BindingAdapter("bindList")
- https://gist.github.com/aucd29/06f28df979cde7fdff3f9db2f32f43bd
- xml 내의 view 끼리도 참조시 참조할 아이디가 @+id/hello_world 라면 @{helloWorld} 식으로 참조 가능 (아이디를 카멜 형태로)
ViewPagerBindingAdapter
- https://gist.github.com/aucd29/28e7c50e9b9706f79525e828bd8e9c87
implementing MVVM using LiveData, RxJava, Dagger Android
- https://proandroiddev.com/mvvm-architecture-using-livedata-rxjava-and-new-dagger-android-injection-639837b1eb6c
Building Offline-First App using MVVM, RxJava, Room and Priority Job Queue
- https://proandroiddev.com/offline-apps-its-easier-than-you-think-9ff97701a73f