question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Touch event is not emitted when handled func returns false in ViewTouchOnSubscribe

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:7
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
ZakTaccardicommented, Apr 6, 2017

That overload already exists: RxView.touches(view, e -> false)

If the e -> false predicate returns false, .onNext() doesn’t get called.

My use case is that I want to observe touch events without consuming them.

    @Override public boolean onTouch(View v, MotionEvent event) {
      if (!isDisposed()) {
        try {
          if (handled.test(event)) {
            observer.onNext(event); //no on next if predicate is false
            return true;
          }
        } catch (Exception e) {
          observer.onError(e);
          dispose();
        }
      }

      return false;

From ViewTouchObservable

2reactions
DrewCarlsoncommented, Apr 18, 2016

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage touch events in a ViewGroup - Android Developers
If onInterceptTouchEvent() returns true , the MotionEvent is intercepted, ... false // Do not intercept touch event, let the child handle it
Read more >
Touch events - Web APIs - MDN Web Docs - Mozilla
When a touchstart event occurs, indicating that a new touch on the surface has occurred, the handleStart() function below is called. function ......
Read more >
Touch Event Not Returning Touch Data - Stack Overflow
There are two issues at play here: 1) When using jQuery, you have to use originalEvent (as VSO notes here) 2) When inspecting...
Read more >
KYOCERA Hydro XTRM
The home screen is the starting point for all applications, functions, and menus. From any application screen, touch Home to return to the...
Read more >
Introduction - Buick
The RKE transmitter functions may work up ... on the driver door handle will unlock the ... When replacing the battery, do not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found