ViewTreeObserver中的OnGlobalLayoutListener

本文介绍了ViewTreeObserver类及其OnGlobalLayoutListener接口的功能与用途。ViewTreeObserver用于注册监听器,以便在视图树发生变化时得到通知,如整体布局更新、触摸模式改变等。OnGlobalLayoutListener则是在视图树全局布局状态或视图可见性变化时被调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * A view tree observer is used to register listeners that can be notified of global
 * changes in the view tree. Such global events include, but are not limited to,
 * layout of the whole tree, beginning of the drawing pass, touch mode change....
 *
 * A ViewTreeObserver should never be instantiated by applications as it is provided
 * by the views hierarchy. Refer to {@link android.view.View#getViewTreeObserver()}
 * for more information.
 */
public final class ViewTreeObserver

ViewTreeObserver类用来注册各种view tree变化的监听。

/**
 * Interface definition for a callback to be invoked when the global layout state
 * or the visibility of views within the view tree changes.
 */
public interface OnGlobalLayoutListener {
    /**
     * Callback method to be invoked when the global layout state or the visibility of views
     * within the view tree changes
     */
    public void onGlobalLayout();
}

OnGlobalLayoutListener 接口:

当全局布局状态或视图树中视图的可见性发生更改时,将调用的回调的接口定义。

一般来说,在onCreate时我们是无法拿到View的高度和宽度的,可以听过监听这个接口来获取高度和宽度,或者做一些View的动画处理之类的。

itemsView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			
    @Override
    public void onGlobalLayout() {
        itemsView.postDelayed(runnable, 10);
        itemsView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值