Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Your First MVVM with RxJava in Android
Search
Elvis Lin
March 20, 2017
Programming
0
840
Your First MVVM with RxJava in Android
Demonstrate how to use RxJava to implement MVVM pattern in Android
Elvis Lin
March 20, 2017
Tweet
Share
More Decks by Elvis Lin
See All by Elvis Lin
Protect Users' Privacy in iOS 14
elvismetaphor
0
46
Dubugging Tips and Tricks for iOS development
elvismetaphor
0
49
Strategies of Facebook LightSpeed project
elvismetaphor
0
63
Background Execution And WorkManager
elvismetaphor
2
480
作為一個跨平台的 Mobile App 開發者,從入門到放棄!?
elvismetaphor
2
480
Dependency Injection for testability of iOS app
elvismetaphor
1
1.4k
Briefly Introduction of Kotlin coroutines
elvismetaphor
1
270
MotionLayout Brief Introduction
elvismetaphor
1
320
Chapter 10. Pattern Matching with Regular Expressions
elvismetaphor
0
38
Other Decks in Programming
See All in Programming
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
370
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
200
Fixstars高速化コンテスト2024準優勝解法
eijirou
0
190
月刊 競技プログラミングをお仕事に役立てるには
terryu16
1
1.2k
QA環境で誰でも自由自在に現在時刻を操って検証できるようにした話
kalibora
1
140
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
430
ドメインイベント増えすぎ問題
h0r15h0
2
570
はてなにおけるfujiwara-wareの活用やecspressoのCI/CD構成 / Fujiwara Tech Conference 2025
cohalz
3
2.8k
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
300
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
420
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
1k
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
210
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
230
52k
How to Think Like a Performance Engineer
csswizardry
22
1.3k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Making the Leap to Tech Lead
cromwellryan
133
9k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
How GitHub (no longer) Works
holman
312
140k
How STYLIGHT went responsive
nonsquared
96
5.3k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.5k
Transcript
使⽤用 RxJava 實作 MVVM 模式 Elvis Lin 2017-03-20 @Android
Taipei
Agenda • MVC 與 MVP 回顧 • MVVM 簡介 •
⽤用 RxJava 實作 MVVM
MVC/MVP回顧
MVC Model - View - Controller
View Controller Model User Input Update Model Query Data Update
UI Dispatch Event
View Controller Model User Input 1 … 1 Activity
View Controller Model User Input Update Model Query data Update
UI Inform about input Notify view
View Controller Model 1 … 1 View Inferface
MVP Model - View - Presenter
View Presenter Update UI Inform User Action
View Presenter Model 1 … 1 IView IPresenter Contract
interface IView { void setCapital(String capital) }
interface IPresenter { void onLoad(); }
MVVM簡介
MVVM Model - View - ViewModel
View ViewModel Model 1 … * View ViewModel Model *
… *
• View 向 ViewModel 註冊 • ViewModel 不主動更更新 View •
ViewModel 不知道 View 的存在 • 實作⽅方式: • Data-binding • RxJava
RxJava 簡介
• 使⽤用 Observer pattern • 提供便便利利的 Thread 控制 • Observer
建立的⽅方式 • Observer.create() • Observer.just() • ……
⽤用 RxJava 實現 MVVM
View ViewModel Model RxJava
None
建立⼀一個 ViewModel public class SearchViewModel { private BehaviorSubject<String> scoreValue; public
SearchViewModel() { scoreValue = BehaviorSubject.create(); } public void searchScore(final String userName) { // do something } public Observable<String> getScoreValueObservable() { return scoreValue; } }
在 View 中對 ViewModel 註冊 public class SearchScoreActivity extends AppCompatActivity
{ @Override protected void onCreate(Bundle savedInstanceState) { searchViewModel = new SearchViewModel(); } @Override protected void onResume() { searchViewModel.getScoreValueObservable(). observeOn(AndroidSchedulers.mainThread()). subscribe(new Observer<String>() { @Override public void onNext(String value) { updateScoreView(value); } }); } }
DEMO https://github.com/elvismetaphor/SearchScore-RX- MVVM
結論 • 關注點分離(Separation of Concerns ) • MVVM 讓 View
跟 ViewModel 單向/雙向綁定 • View 只負責資料的顯⽰示跟UI事件傳遞 • View 根據 ViewModel 的變化,更更新⾃自⼰己的畫⾯面 • ViewModel 不知道 View 的存在
Contact Info Elvis Lin • Github: https://github.com/elvismetaphor • Blog:
http://article.elvismetaphor.me • Slides: https://speakerdeck.com/ elvismetaphor
參參考資料 • 投影片中的範例例 https://github.com/elvismetaphor/ SearchScore-RX-MVVM • Google Architecture 範例例 https://github.com/googlesamples/android-
architecture • MVP/MVVM 範例例 https://github.com/ivacf/archi
補充資料 (1/2) • RxJava 2.0 介紹 @Kros https://www.slideshare.net/ssuser72c3b0/ rxjava-20 •
MVP in Practice @洪彥斌 https://www.slideshare.net/ssuser8674c1/ mvp-in-practice
補充資料 (2/2) • Introduction to MV(Whatever) in Android https://speakerdeck.com/elvismetaphor/ introduction-to-mv-whatever-in-android
• 使⽤用 Data binding 實作 MVVM 模式 https://speakerdeck.com/elvismetaphor/ introduction-to-mv-whatever-in-android
None