rxbinding 并单击右侧的可绘制对象
rxbinding and click on right drawable
有没有办法使用 RxBinding 在 EditText 的右侧可绘制对象上实现点击监听器?
我唯一找到的是:
RxTextView.editorActionEvents(mEditText).subscribeWith(new DisposableObserver<TextViewEditorActionEvent>() {
@Override
public void onNext(TextViewEditorActionEvent textViewEditorActionEvent) {
int actionId = textViewEditorActionEvent.actionId();
if(actionId == MotionEvent.ACTION_UP) {
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
但是在这个事件中我找不到关于点击位置的信息。
这是我使用 RxJava 实现的方式:
public Observable<Integer> getCompoundDrawableOnClick(EditText editText, int... drawables) {
return Observable.create(e -> {
editText.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
for (int i : drawables) {
if (i == UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT) {
if (event.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[i].getBounds().width())) {
e.onNext(i);
return true;
}
}
}
}
// add the other cases here
return false;
});
});
但我觉得我正在重新发明轮子
你在错误的地方搜索,如果你需要检查触摸事件,使用基础 View
触摸事件使用 RxView
,然后应用你的逻辑并过滤掉所有不需要的感动,因为 'clicks' 在你想要的位置(复合可绘制)。
我必须承认我不确定我是否理解 for 循环逻辑,你可以直接使用 UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT
,但无论如何在这个例子中遵循你的逻辑:
public Observable<Object> getCompoundDrawableOnClick(EditText editText, int... drawables) {
return RxView.touches(editText)
.filter(motionEvent -> {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
for (int i : drawables) {
if (i == UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT) {
if (motionEvent.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[i].getBounds().width())) {
return true;
}
}
}
}
return false;
})
.map(motionEvent -> {
// you can omit it if you don't need any special object or map it to
// whatever you need, probably you just want click handler so any kind of notification Object will do.
});
}
有没有办法使用 RxBinding 在 EditText 的右侧可绘制对象上实现点击监听器?
我唯一找到的是:
RxTextView.editorActionEvents(mEditText).subscribeWith(new DisposableObserver<TextViewEditorActionEvent>() {
@Override
public void onNext(TextViewEditorActionEvent textViewEditorActionEvent) {
int actionId = textViewEditorActionEvent.actionId();
if(actionId == MotionEvent.ACTION_UP) {
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
但是在这个事件中我找不到关于点击位置的信息。
这是我使用 RxJava 实现的方式:
public Observable<Integer> getCompoundDrawableOnClick(EditText editText, int... drawables) {
return Observable.create(e -> {
editText.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
for (int i : drawables) {
if (i == UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT) {
if (event.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[i].getBounds().width())) {
e.onNext(i);
return true;
}
}
}
}
// add the other cases here
return false;
});
});
但我觉得我正在重新发明轮子
你在错误的地方搜索,如果你需要检查触摸事件,使用基础 View
触摸事件使用 RxView
,然后应用你的逻辑并过滤掉所有不需要的感动,因为 'clicks' 在你想要的位置(复合可绘制)。
我必须承认我不确定我是否理解 for 循环逻辑,你可以直接使用 UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT
,但无论如何在这个例子中遵循你的逻辑:
public Observable<Object> getCompoundDrawableOnClick(EditText editText, int... drawables) {
return RxView.touches(editText)
.filter(motionEvent -> {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
for (int i : drawables) {
if (i == UiUtil.COMPOUND_DRAWABLE.DRAWABLE_RIGHT) {
if (motionEvent.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[i].getBounds().width())) {
return true;
}
}
}
}
return false;
})
.map(motionEvent -> {
// you can omit it if you don't need any special object or map it to
// whatever you need, probably you just want click handler so any kind of notification Object will do.
});
}