Last active
July 26, 2019 16:32
-
-
Save thanhtoan1196/be4d60dc1d162eaa32789a63a27431ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RxView.clicks(btLogin) | |
.subscribe(new Action1<Void>() { | |
@Override | |
public void call(Void aVoid) { | |
Toast.makeText(MainActivity.this,"开始登录",Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
RxCompoundButton.checkedChanges(checkBox) | |
.subscribe(new Action1<Boolean>() { | |
@Override | |
public void call(Boolean aBoolean) { | |
btLogin.setEnabled(aBoolean); | |
btLogin.setBackgroundColor(aBoolean? Color.BLUE:Color.WHITE); | |
} | |
}); | |
Observable<CharSequence> usernameObservable= RxTextView.textChanges(edUsername).skip(1); | |
Observable<CharSequence> passwordObservable= RxTextView.textChanges(edPassword).skip(1); | |
Observable.combineLatest(usernameObservable, passwordObservable, | |
new Func2<CharSequence, CharSequence, Boolean>() { | |
@Override | |
public Boolean call(CharSequence username, CharSequence password) { | |
Boolean isusernameValid=!(username.toString().length()==0); | |
if (!isusernameValid){ | |
edUsername.setError("用户名无效,2-9个字符"); | |
} | |
Boolean ispasswordValid=!(password.toString().length()==0); | |
if (!ispasswordValid){ | |
edPassword.setError("密码无效,2-9个字符"); | |
} | |
return isusernameValid&&ispasswordValid; | |
} | |
}) | |
.subscribeOn(AndroidSchedulers.mainThread()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Subscriber<Boolean>() { | |
@Override | |
public void onCompleted() { | |
} | |
@Override | |
public void onError(Throwable e) { | |
} | |
@Override | |
public void onNext(Boolean aBoolean) { | |
btRegister.setEnabled(aBoolean); | |
btRegister.setBackgroundColor(aBoolean? Color.BLUE:Color.WHITE); | |
} | |
}); | |
RxView.clicks(btnRefresh) | |
.throttleWithTimeout(500, TimeUnit.MILLISECONDS) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.doOnNext(new Action1<Void>() { | |
@Override | |
public void call(Void aVoid) { | |
contributors.clear(); | |
recyclerViewAdapter.notifyDataSetChanged(); | |
} | |
}) | |
.subscribe(new Subscriber<Object>() { | |
@Override | |
public void onCompleted() { | |
Toast.makeText(MainActivity.this, "onCompleted", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onError(Throwable e) { | |
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onNext(Object object) { | |
Toast.makeText(MainActivity.this, "onNext", Toast.LENGTH_SHORT).show(); | |
ServiceGenerator.createService().repoContributor4Observable("square", "retrofit") | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Subscriber<List<Contributor>>() { | |
@Override | |
public void onCompleted() { | |
} | |
@Override | |
public void onError(Throwable e) { | |
} | |
@Override | |
public void onNext(List<Contributor> contributorList) { | |
contributors.addAll(contributorList); | |
recyclerViewAdapter.notifyDataSetChanged(); | |
} | |
}); | |
} | |
}); | |
Observable<String> observable = Observable.create(mSubscribeAction); | |
observable.observeOn(AndroidSchedulers.mainThread()); | |
observable.subscribe(mToastSubscriber); | |
Observable.OnSubscribe mSubscribeAction = new Observable.OnSubscribe<String>() { | |
@Override | |
public void call(Subscriber<? super String> subscriber) { | |
subscriber.onNext(sayHello()); | |
subscriber.onCompleted(); | |
} | |
}; | |
Subscriber<String> mToastSubscriber = new Subscriber<String>() { | |
@Override | |
public void onCompleted() { | |
} | |
@Override | |
public void onError(Throwable e) { | |
} | |
@Override | |
public void onNext(String s) { | |
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); | |
} | |
}; | |
RxTextView.textChanges(editText) | |
.subscribe(charSequence -> { | |
textView.setText(charSequence); | |
}); | |
RxToolbar.navigationClicks(toolbar).subscribe(new Action1<Void>() { | |
@Override | |
public void call(Void aVoid) { | |
onBackPressed(); | |
} | |
}); | |
RxView.clicks(mPreventMultipleClicksBtn3).debounce(1000, TimeUnit.MILLISECONDS) | |
.subscribe(new Action1<Void>() { | |
@Override | |
public void call(Void aVoid) { | |
Log.d(TAG, "Response while clicks interval more than 1s"); | |
} | |
}); | |
RxView.clicks(mDelayResponseBtn) | |
.delay(1000, TimeUnit.MILLISECONDS) | |
.subscribe(new Action1<Void>() { | |
@Override | |
public void call(Void aVoid) { | |
Log.d(TAG, "Response the click after 1s. response time = " + System.currentTimeMillis()); | |
} | |
}); | |
Subscription seekBarSub = RxSeekBar.userChanges(colorSlider).subscribe(new Action1<Integer>() { | |
@Override | |
public void call(Integer seekValue) { | |
viewContainer.setBackgroundColor(Color.argb(seekValue, 131, 255, 8)); | |
} | |
}); | |
compositeSubscription.add(seekBarSub); | |
Observable<Void> clickObservable = RxView.clicks(button).share(); | |
Subscription loggingSub = clickObservable.subscribe(new Action1<Void>() { | |
@Override | |
public void call(Void aVoid) { | |
Timber.d("Button was clicked!"); | |
} | |
}); | |
compositeSubscription.add(loggingSub); | |
compositeSubscription.unsubscribe(); | |
RxTextView.textChanges(mSearchUserEditText) | |
.debounce(Constants.DEBOUNCE, TimeUnit.MILLISECONDS) | |
.map(CharSequence::toString) | |
.map(String::trim) | |
.switchMap(username -> apiManager.getUserReposWithObservable(username) | |
.doOnError(this::showError) | |
.onErrorResumeNext(Observable.empty()) | |
) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(updateRepos(), this::showError); | |
RxSwipeRefreshLayout.refreshes(binding.refresher) | |
.compose(bindToLifecycle()) | |
.flatMap(whatever -> load) | |
.retry((count, tr) -> { | |
Log.e(TAG, "An error occurred while fetching images", tr); | |
ToastUtil.shorts(this, tr.getMessage()); | |
binding.refresher.setRefreshing(false); | |
return true; | |
}) | |
.filter(loaded -> !loaded.isEmpty()) | |
.subscribe(loaded -> binding.content.smoothScrollToPosition(0)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment