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
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
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
69
Dubugging Tips and Tricks for iOS development
elvismetaphor
0
81
Strategies of Facebook LightSpeed project
elvismetaphor
0
130
Background Execution And WorkManager
elvismetaphor
2
560
作為一個跨平台的 Mobile App 開發者,從入門到放棄!?
elvismetaphor
2
590
Dependency Injection for testability of iOS app
elvismetaphor
1
1.5k
Briefly Introduction of Kotlin coroutines
elvismetaphor
1
340
MotionLayout Brief Introduction
elvismetaphor
1
400
Chapter 10. Pattern Matching with Regular Expressions
elvismetaphor
0
83
Other Decks in Programming
See All in Programming
Simple Storage Service(S3) is not simple
iwatsukayura
0
290
contenteditable と日本語入力に向き合う
colorful12
0
200
ジョジョの世界をXRで再現する
mofurune
0
170
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
1k
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
160
Web Framework Benchmark Battle 2026, Any%
a_matsuda
2
2.2k
R8 の設定見直しでアプリサイズ削減
andpad
0
220
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
570
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
350
Workers Cache を知る
syumai
0
370
SalesForceを内製化!? ~ HR事業を支える顧客管理基盤のインフラを大公開 ~
oku053
0
180
Next.js: The final boss of Web Performance
guaca
0
260
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Skip the Path - Find Your Career Trail
mkilby
1
260
The Mindset for Success: Future Career Progression
greggifford
PRO
0
540
Everyday Curiosity
cassininazir
0
350
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
730
Rebuilding a faster, lazier Slack
samanthasiow
85
9.7k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
The Curse of the Amulet
leimatthew05
3
16k
ラッコキーワード サービス紹介資料
rakko
1
5.4M
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
RailsConf 2023
tenderlove
30
1.6k
Leo the Paperboy
mayatellez
10
2.4k
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