Debounce1. Official definition only emit an item from an Observable if a particular timespan have passed without it emitting another item
The debounce operator filters out items emitted by the source Observable that is rapidly followed by another emitted Item.
2.API
Public Final Observable<t> debounce (long timeout, timeunit unit); // Default Execution Thread schedulers.computation () Public Final Observable<t> debounce (long timeout, timeunit unit, Scheduler Scheduler);
Using scenes in 3.Android
Click the button quickly to perform an action.
For example, the selection package in the American group app: the 0-part Quick Click on the left image to 7 copies of the right, then the total price based on the number of selected copies.
4. The code implements a
// Numberpickerview.java -> { selectcount++ ; + ""); Onchangelistener.onchange (Dealid, selectcount); // Dealid is the ID of the current package }); Public Interface Onchangelistener { onChange (intint selectcount);}
// Activity { calculatedealprice (dealid, SelectCount);}); private calculatedealprice (intint selectcount) { // calculated price }
For this quick click, we actually need to calculate the 7th time, the middle of a series of transient state is not necessary to calculate, using debounce to solve.
5. Code implementation Two: Increase debounce operation
rxview.clicks (plusview) { SelectCount+ +; + ""); return selectcount; } . Debounce (timeunit.milliseconds)) . Observeon (Androidschedulers.mainthread ( ))- > Onchangelistener.onchange (Dealid, SelectCount), Throwable::p rintstacktrace);
Disadvantages:
1.NumberPickerView relies on the com.jakewharton.rxbinding:rxbinding:x.x.x
2.NumberPickerView Plusview is forced to increase the debounce operation of 400ms
5. Code implementation Three: Move the debounce operation out of the Numberpickerview
// Numberpickerview.java -> { selectcount++ ; + ""); Onchangelistener.onchange (Dealid, selectcount); // Dealid is the ID of the current package});
//Activity... Publishsubject<SelectParams> subject =publishsubject.create (); Numberpickerview.setonchangelistener (Dealid, SelectCount)-{Subject.onnext (Newselectparams (Dealid, SelectCount);}); Subject.debounce (400, Timeunit.milliseconds). Observeon (Androidschedulers.mainthread ()). Subscribe (Selectparams-Calculatedealprice (Selectparams.dealid, Selectparams.selectcount), Throwable::p rintstacktrace);classSelectparams {intDealid; intSelectCount; Selectparams (intDealid,intSelectcont) { This. Dealid =Dealid; This. SelectCount =SelectCount; }}PrivateCalculatedealprice (intDealid,intSelectCount) { ... //Calculate Price}
At this point Numberpickerview no longer relies on third-party libraries for improved applicability.
Reference: http://reactivex.io/documentation/operators.html
Rxjava debounce operator