-
Notifications
You must be signed in to change notification settings - Fork 971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RxView scrollChangeEvents for api lower than M #315
Comments
Send a PR if you like.
…On Mon, Dec 5, 2016, 10:09 AM MickSparrow ***@***.***> wrote:
For now scroll change events are supported only for @TargetApi(M).
@TargetApi(M)
@checkresult @nonnull
public static Observable<ViewScrollChangeEvent> ***@***.*** View view) {
checkNotNull(view, "view == null");
return Observable.create(new ViewScrollChangeEventOnSubscribe(view));
}
I write simple solution for this:
public static Observable<ViewScrollChangeEvent> scrollChangeEvents(View view) {
checkNotNull(view, "view == null");
return Observable.create(new CustomScrollChangeEventOnSubscribe(view));
}
final class CustomScrollChangeEventOnSubscribe implements Observable.OnSubscribe<ViewScrollChangeEvent> {
private final View view;
private ViewTreeObserver.OnScrollChangedListener onScrollChangedListener;
private ViewTreeObserver viewTreeObserver;
CustomScrollChangeEventOnSubscribe(View view) {
this.view = view;
}
@OverRide
public void call(final Subscriber<? super ViewScrollChangeEvent> subscriber) {
checkUiThread();
ViewScrollChangeEventListener listener = new ViewScrollChangeEventListener() {
@OverRide
public void onViewScrollChangeEvent(ViewScrollChangeEvent viewScrollChangeEvent) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(viewScrollChangeEvent);
}
}
};
setScrollTouchListener(listener, view);
subscriber.add(new MainThreadSubscription() {
@OverRide
protected void onUnsubscribe() {
if (checkTreeObserver()) {
viewTreeObserver.removeOnScrollChangedListener(onScrollChangedListener);
}
}
});
}
private void setScrollTouchListener(final ViewScrollChangeEventListener viewScrollChangeEventListener, final View viewToObserve) {
onScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
private int oldScrollX = 0;
private int oldScrollY = 0;
@OverRide
public void onScrollChanged() {
if (viewToObserve.getScrollX() != oldScrollX || viewToObserve.getScrollY() != oldScrollY) {
viewScrollChangeEventListener.onViewScrollChangeEvent(ViewScrollChangeEvent.create(viewToObserve, viewToObserve.getScrollX(), viewToObserve.getScrollY(), oldScrollX, oldScrollY));
oldScrollX = viewToObserve.getScrollX();
oldScrollY = viewToObserve.getScrollY();
}
}
};
viewTreeObserver = viewToObserve.getViewTreeObserver();
if (checkTreeObserver()) {
viewTreeObserver.addOnScrollChangedListener(onScrollChangedListener);
}
}
private boolean checkTreeObserver() {
return viewTreeObserver != null && viewTreeObserver.isAlive();
}
interface ViewScrollChangeEventListener {
void onViewScrollChangeEvent(ViewScrollChangeEvent viewScrollChangeEvent);
}
}
Maybe it can be added to library
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#315>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEEEfX_HfC5e7XDI0kb76HPgTiJ8huiks5rFCk2gaJpZM4LEVcA>
.
|
OK, this solution is not passing test, but passes "eye test", so I must check it out, and when I'll send PR. |
I tested my solution on some devices with api level from 16 to 23 and its working. |
@MickSparrow
In test case, view doesn't attach any contexts. So view.getViewTreeObserver() returns mFloatingTreeObserver. It doesn't invoke ViewTreeObserver.dispatchOnScrollChanged RxViewText.java View.java
So I added new test code RxViewScrollTest.java. It tests RxView.scrollChangeEvents with view attached a activity context. RxViewScrollTestActivity.java
setContentView(view); solves this problem
ViewTreeObserver.dispatchOnScrollChanged called by Choreographer$FrameHandler
|
@JakeWharton But I just made small changes, then 03685a0 failed. I have no idea why it failed with the Classes that I didn't touch. com.jakewharton.rxbinding2.view.RxViewSystemUiVisibilityTest > systemUiVisibilityChanges[test(AVD) - 4.3.1] FAILED https://travis-ci.org/JakeWharton/RxBinding/jobs/345882826#L1667 |
It seems to be flaky tests, so I just leave it failed. |
For now scroll change events are supported only for @TargetApi(M).
I write simple solution for this:
Maybe it can be added to library
The text was updated successfully, but these errors were encountered: