Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Your First MVVM with RxJava in Android
Search
Elvis Lin
March 20, 2017
Programming
940
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
62
Dubugging Tips and Tricks for iOS development
elvismetaphor
0
79
Strategies of Facebook LightSpeed project
elvismetaphor
0
120
Background Execution And WorkManager
elvismetaphor
2
530
作為一個跨平台的 Mobile App 開發者,從入門到放棄!?
elvismetaphor
2
570
Dependency Injection for testability of iOS app
elvismetaphor
1
1.5k
Briefly Introduction of Kotlin coroutines
elvismetaphor
1
330
MotionLayout Brief Introduction
elvismetaphor
1
380
Chapter 10. Pattern Matching with Regular Expressions
elvismetaphor
0
80
Other Decks in Programming
See All in Programming
週末にAI-DLCを本気で回したら$1,600溶けた
hbashimizu
0
130
[GoCon2026] When Goroutines Are Not Enough: Runtime Locality in High-Throughput Go
takehaya
5
690
デプロイ直後のレイテンシスパイクを調べたら、 Railsの仕様にたどり着いた
nhsykym
0
110
型解析で実現する Go の言語内 DSL / Conference に Go! タイムテーブルの歩き方 for Gophers
mazrean
0
130
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
160
ALB ログから Trace を気合で繋げる技術
fohte
7
880
マイコン向けの軽量Ruby「PicoRuby」で各種デバイスを制御するネイティブアプリの実現手法
bash0c7
0
150
アクセシビリティから考える情報設計
high_g_engineer
0
320
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
150
AIに既存システムを理解させる技術 ~レガシーを見捨てないハーネスエンジニアリング入門~
ochtum
0
210
GKE アップグレード前に知っておきたい Blue/Green と PDB の関係
stkk
0
160
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
180
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
330
Design in an AI World
tapps
1
310
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.3k
How to train your dragon (web standard)
notwaldorf
97
6.8k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
600
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
450
Visualization
eitanlees
152
17k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
530
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
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