dwarvesf / til

Today I Learned. Our knowledge hub. List out what we've learned everyday, organized.
29 stars 1 forks source link

RxView.globalLayouts RxView.globalLayouts replace addOnGlobalLayoutListener #51

Open siliconprime-thanhduc opened 5 years ago

siliconprime-thanhduc commented 5 years ago

Khi ta cần detect layout change trước đây ta thường dùng

myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
    }
});

Chúng ta có thể dùng RxView để làm việc này 1 cách dễ dàng hơn:

 RxView.globalLayouts(info_overlay)
        .map { info_overlay.visibility }
        .distinct()
        .subscribe { newVisibility ->
        }

hoặc

 RxView.layoutChanges(view)
            .distinct()
            .subscribe { newVisibility ->
            }