我在我的项目中使用RxJava2,Kotlin-1.1和RxBindings.
我有一个简单的登录界面,默认情况下禁用"登录"按钮,我想只在用户名和密码的edittext字段不为空时启用该按钮.
LoginActivity.java
Observable<Boolean> isFormEnabled =
Observable.combineLatest(mUserNameObservable, mPasswordObservable,
(userName, password) -> userName.length() > 0 && password.length() > 0)
.distinctUntilChanged();
Run Code Online (Sandbox Code Playgroud)
我无法将上述代码从Java翻译成Kotlin:
LoginActivity.kt
class LoginActivity : AppCompatActivity() {
val disposable = CompositeDisposable()
private var userNameObservable: Observable<CharSequence>? = null
private var passwordObservable: Observable<CharSequence>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
initialize()
}
fun initialize() {
userNameObservable = RxTextView.textChanges(username).skip(1)
.debounce(500, TimeUnit.MILLISECONDS)
passwordObservable = RxTextView.textChanges(password).skip(1)
.debounce(500, TimeUnit.MILLISECONDS)
}
private fun setSignInButtonEnableListener() {
val isSignInEnabled: Observable<Boolean> = Observable.combineLatest(userNameObservable,
passwordObservable,
{ u: CharSequence, p: CharSequence …Run Code Online (Sandbox Code Playgroud) 我有一个简单的用例,其中:
Activity1创建一个fragment1
创建后的fragment1通知活动它是否已创建并更新其activity1视图.
获取通知更新fragment1视图后的activity1.
我正在使用rxandroid,sublibrary rxlifecycle组件和android,但我还在学习阶段,rx-lifecyclestackoverflow上甚至没有标记,所以我仍然在努力理解这个库的流程..
编辑
我不喜欢使用EventBus,就像每个人都大喊大叫做某事一样,所以Rxjava Observable方法会很有用
android android-fragments android-activity rx-android rx-binding
我是新的android和rxjava.我已经通过很多例子来讨论使用rxbindings的事件.比如这个
RxView.clicks(b).subscribe(new Action1<Void>() {
@Override
public void call(Void aVoid) {
// do some work here
}
});
Run Code Online (Sandbox Code Playgroud)
要么
RxTextView.textChanges(name)
.subscribe(new Action1<String>() {
@Override
public void call(String value) {
// do some work with the updated text
}
});
Run Code Online (Sandbox Code Playgroud)
现在我想为android微调器做同样的事情.我想听itemselected事件.有人可以帮忙吗?
我当前的Android应用程序允许用户远程搜索内容.
例如,向用户显示一个EditText接受其搜索字符串并触发远程API调用的用户,该调用返回与输入的文本匹配的结果.
更糟糕的情况是,我只需添加一个TextWatcher并在每次调用时触发API调用onTextChanged.这可以通过在进行第一次API调用之前强制用户输入至少N个字符来进行搜索来改进.
"完美"解决方案具有以下特点: -
一旦用户开始输入搜索字符串
定期(每M毫秒)消耗输入的整个字符串.每当周期到期并且当前用户输入与先前的用户输入不同时触发API调用.
[是否有可能与输入的文本长度相关的动态超时?例如,当文本"短"时,API响应大小将很大并且需要更长时间才能返回和解析; 随着搜索文本变得越来越长,API响应大小将随着"飞行"和解析时间而减少
当用户重新键入EditText字段时,重新启动Periodic消耗文本.
每当用户按下ENTER键触发"最终"API调用,并停止监视用户输入到EditText字段.
设置用户在触发API调用之前必须输入的文本的最小长度,但将此最小长度与覆盖的超时值组合,以便在用户希望搜索"短"文本字符串时可以.
我确信RxJava和/或RxBindings可以支持上述要求,但到目前为止我还没有实现一个可行的解决方案.
我的尝试包括
private PublishSubject<String> publishSubject;
publishSubject = PublishSubject.create();
publishSubject.filter(text -> text.length() > 2)
.debounce(300, TimeUnit.MILLISECONDS)
.toFlowable(BackpressureStrategy.LATEST)
.subscribe(new Consumer<String>() {
@Override
public void accept(final String s) throws Exception {
Log.d(TAG, "accept() called with: s = [" + s + "]");
}
});
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
}
@Override …Run Code Online (Sandbox Code Playgroud) 当用户键入SearchView窗口小部件时,应用程序应进行API调用(在后台线程中)以从服务器获取搜索结果,并在RecyclerView中显示它们(在UI线程中).
我在我的片段中使用以下代码:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_fragment, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
RxSearchView.queryTextChanges(searchView)
.debounce(400, TimeUnit.MILLISECONDS)
.map(CharSequence::toString)
.switchMap(query -> retrofitService.search(query))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<Item>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.e(LOG_TAG, "Error", e);
}
@Override
public void onNext(List<Item> items) {
// adapter.addItems(...)
}
});
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个例外:
java.lang.IllegalStateException: Must be called from the main thread. Was: Thread[RxIoScheduler-2,5,main]
at …Run Code Online (Sandbox Code Playgroud) 我如何在Kotlin中使用RxBinding:
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
reset_password_text_view.clicks().subscribe { presenter.showConfirmSecretQuestionBeforeResetPassword() }
password_edit_text.textChanges().skip(1).subscribe { presenter.onPasswordChanged(it.toString()) }
password_edit_text.editorActionEvents().subscribe { presenter.done(password_edit_text.text.toString()) }
}
Run Code Online (Sandbox Code Playgroud)
Observable.subscribe(action)回报Subscription.我应该保留它作为参考并取消订阅onPause()或onDestroy()?
像这样:
private lateinit var resetPasswordClicksSubs: Subscription
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
resetPasswordClicksSubs = reset_password_text_view.clicks().subscribe { presenter.showConfirmSecretQuestionBeforeResetPassword() }
}
override fun onDestroy() {
super.onDestroy()
resetPasswordClicksSubs.unsubscribe()
}
Run Code Online (Sandbox Code Playgroud) 这是我的gradle依赖项.
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:27.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
//Recycleview
implementation 'com.android.support:recyclerview-v7:27.0.2'
//Butterknife
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//SDP
implementation 'com.intuit.sdp:sdp-android:1.0.5'
//OkHttp
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
//RxJava and RxAndroid
implementation 'io.reactivex.rxjava2:rxjava:2.0.6'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.ashdavies.rx:rx-firebase:1.3.3'
//RxBinding
implementation 'com.jakewharton.rxbinding:rxbinding-design:0.4.0'
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
//Glide
implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
//GSON
implementation 'com.google.code.gson:gson:2.2.4'
//Image Crop Library
implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.+'
//Dagger Android
implementation 'com.google.dagger:dagger:2.13'
annotationProcessor 'com.google.dagger:dagger-compiler:2.13'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.13'
implementation 'com.google.dagger:dagger-android-support:2.13' …Run Code Online (Sandbox Code Playgroud) android android-multidex android-coordinatorlayout rx-binding
我正在尝试使用MVP架构在Android应用程序中实现一个屏幕,并在View端使用RxJava和RxBinding.
基本上我有2个Spinners,1个TextEdit和一个默认禁用的按钮.我想在Spinners选择了项目且文本字段不为空时启用该按钮.这是代码:
Observable.combineLatest(
RxAdapterView.itemSelections(mFirstSpinner),
RxAdapterView.itemSelections(mSecondSpinner),
RxTextView.textChanges(mEditText),
new Func3<Integer, Integer, CharSequence, Boolean>() {
@Override
public Boolean call(Integer first, Integer second, CharSequence value) {
return !TextUtils.isEmpty(value);
}
}).subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean enable) {
mButton.setEnabled(enable);
}
});
Run Code Online (Sandbox Code Playgroud)
现在的问题是如何将其整合到MVP模式中.理想情况下,启用按钮的"业务逻辑"应该在演示者中.实现这一目标的最佳方法是什么?我正在考虑将原始观察者以某种方式传递给演示者(侧面问题是如何?),演示者将组合这些观察者,并且它将具有启用按钮的逻辑.最后,它只会调用View来修改按钮状态.
还有更好的选择吗?在View端有没有RxJava MVP的好例子?
RxBinding 没有自己的文档。这些命令对于专业开发人员来说可能很容易,但对于初学者来说却不是。有没有人有自我记录的文档或有充足资源的链接?
我是新手,我要求最好的正确方法来处理 Retrofit 使用中的所有可能状态,其中Rxjava包括:Retrofitrxjavarxbinding
Username or password is incorrect。connection reset by peers.rx-binding ×10
android ×9
rx-java ×5
rx-android ×4
rx-java2 ×3
kotlin ×2
java ×1
mvp ×1
retrofit ×1
retrofit2 ×1