preface

  • RxjavaBecause of itsChain call based on event flow, simple logic & easy to useThe characteristics of the deepAndroidDeveloper welcome.


Making screenshots

If you are not familiar with RxJava, check out Android: a clear and easy-to-understand introduction to RxJava

  • RxJavaThat’s why it’s so popularProvides rich & powerful operators that can fulfill almost all functional requirements
  • Today, I’m going to bring youRxjavaOf the create operatorCommon development application scenarios: Function anti-shake requirements, and combined withRetrofitRxJavaImplementation, I hope you like it.
  1. This series of articles is based onRxjava 2.0
  2. In the coming days, I will continue to publish a series of articles on Rxjava 2.0 in Android, including principles, operators, application scenarios, back pressure, etc. If you are interested, please continue to follow Carson_Ho’s Android development notes!!


Schematic diagram


directory


Schematic diagram


1. Requirement scenario


Schematic diagram


2. Function Description


Schematic diagram

Note: The throttleFirst () operator in Rxjava is fully explained in Android Rxjava


3. Implementation

// Register control Button Button; button = (Button)findViewById(R.id.button); Click (button); click (button); click (button); click (button); click (button); The compile 'com. Jakewharton. Rxbinding2: rxbinding: 2.0.0' * 2. Pass in a Button control that sends data events whenever it is clicked (but because of the throttleFirst () operator, **/ RxView. Clicks (button). ThrottleFirst (2, Timeunit.seconds).subscribe(new Observer<Object>() {@override public void subscribe(Disposable) D) {} @override public void onNext(Object value) {log. d(TAG, "sent network request"); } @override public void onError(Throwable e) {log. d(TAG, "response to Error" + e.tostring ()); } @override public void onComplete() {log.d (TAG, "respond to Complete event "); }});Copy the code
  • The test results

    2sNo matter how many times you click, only one network request is sent

    Schematic diagram

4. The Demo address

Carson_Ho Github address = RxJava2 Combat series: function anti – shake


5. To summarize

  • This article mainly explainsRxjavaThe actual development requirement scenario: function anti-shake requirement
  • Now I’m going to combineActual scenario application &RxjavaRelevant use frameworks (e.gRetrofit,Eventbus), continue toAndroidIn theRxjavaThe actual development requirements scenario for in-depth explanation, interested can continue to pay attention toCarson_Ho android Development Notes

Thumb up, please! Because your encouragement is the biggest power that I write!

Related articles reading

  • Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: create operator Android Rxjava: Android RxJava: A comprehensive tutorial on the functional operators
  • Android RxJava application description: (Unconditional) Network request polling Android RxJava application description: (conditional) Network request polling Android RxJava application description: Network request nested callback Android RxJava: merge data source Android RxJava: merge data from disk/memory cache: merge data from disk/memory cache Android RxJava: Network request error reconnection (Retrofit)

Welcome to attentionCarson_HoJane books!

Share the dry things about Android development from time to time, the pursuit of short, flat, fast, but there is no lack of depth.