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
920
0
Share
Your First MVVM with RxJava in Android
Demonstrate how to use RxJava to implement MVVM pattern in Android
Elvis Lin
March 20, 2017
More Decks by Elvis Lin
See All by Elvis Lin
Protect Users' Privacy in iOS 14
elvismetaphor
0
60
Dubugging Tips and Tricks for iOS development
elvismetaphor
0
65
Strategies of Facebook LightSpeed project
elvismetaphor
0
110
Background Execution And WorkManager
elvismetaphor
2
500
作為一個跨平台的 Mobile App 開發者,從入門到放棄!?
elvismetaphor
2
540
Dependency Injection for testability of iOS app
elvismetaphor
1
1.5k
Briefly Introduction of Kotlin coroutines
elvismetaphor
1
320
MotionLayout Brief Introduction
elvismetaphor
1
350
Chapter 10. Pattern Matching with Regular Expressions
elvismetaphor
0
65
Other Decks in Programming
See All in Programming
要はバランスからの卒業 #yumemi_grow
kajitack
0
180
~ 秘伝のタレ化した『神スプシ』と戦う ~ 関数型パラダイムで壊れない仕組みへ
h0r15h0
1
120
「なんか〇〇ライブラリで脆弱性あるみたいなんだけど。。。」から始める脆弱性対応 / First Steps in Vulnerability Response
mackey0225
2
130
RailsTokyo 2026#4: AI様があれば、 Hotwireの弱点は消えるか?
naofumi
3
470
Oxlintはいかにしてtsgolintのlint ruleを呼び出しているのか
syumai
1
360
サプライチェーン攻撃対策「層を重ねて落ちない壁」を10日間で組み上げた話 #TechLeadConf2026
kashewnuts
1
310
PHPでバイナリをパースして理解するASN.1
muno92
PRO
0
460
oxlintはeslint/typescript-eslintを置き換えられるのか
shomafujita
1
100
プロパティの順序で型推論が壊れる!? TypeScript6.0の修正からContext-Sensitivityの仕組みを追う
bicstone
2
520
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
110
Spec-Driven Development with AI Agents (Workshop, May 2026)
antonarhipov
4
410
リセットCSSを1行消したらアクセシビリティが向上した話
pvcresin
4
520
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Ruling the World: When Life Gets Gamed
codingconduct
0
230
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
570
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
460
sira's awesome portfolio website redesign presentation
elsirapls
0
250
The Invisible Side of Design
smashingmag
302
52k
Optimising Largest Contentful Paint
csswizardry
37
3.7k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.5k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
エンジニアに許された特別な時間の終わり
watany
106
240k
WCS-LA-2024
lcolladotor
0
600
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