RxBinding: Touch event is not emitted when handled func returns false in ViewTouchOnSubscribe
Hi. Before creating a PR I would like to ask you guys if I’m right. ViewTouchOnSubscribe class which is internally used during for example RxView.touchesmethod contains:
@Override public void call(final Subscriber<? super MotionEvent> subscriber) {
checkUiThread();
View.OnTouchListener listener = new View.OnTouchListener() {
@Override public boolean onTouch(View v, @NonNull MotionEvent event) {
if (handled.call(event)) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(event);
}
return true;
}
return false;
}
};
view.setOnTouchListener(listener);
subscriber.add(new MainThreadSubscription() {
@Override protected void onUnsubscribe() {
view.setOnTouchListener(null);
}
});
}
Current implementation probably blocks possibility to handle emitted MotionEvent when handled function returns false for example:
RxView.touches(someEditText, motionEvent -> false)
.subscribe(event -> {
// do something here
});
In above case, no value will be emitted. When it’s needed? When we want to detect touch on EditText widget and don’t “consume” the MotionEvent. We can’t consume it if we just want to make some extra action but keep EditText to get focus (that’s why handled have to return false).
My proposed solution:
View.OnTouchListener listener = new View.OnTouchListener() {
@Override public boolean onTouch(View v, @NonNull MotionEvent event) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(event);
}
return handled.call(event);
}
};
Am I correct or I did something wrong?
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 7
- Comments: 15 (3 by maintainers)
If the
e -> falsepredicate returns false,.onNext()doesn’t get called.My use case is that I want to observe touch events without consuming them.
From ViewTouchObservable
Any progress on an approach to take here?
I have a case with nested ViewPagers where the inner ViewPager needs to prevent the parent from changing pages if the inner’s current item is the first or last item. Following this, the inner ViewPager automatically changes it’s current item after a period of time, but this needs to be cancelled on
ACTION_MOVE.Any update on this? I’m having a scenario where longClickEvent is being interrupted by this event.