gpt4 book ai didi

java - 从附加的 OnPreDrawListener 对象中获取 ViewTreeObserver 对象

转载 作者:行者123 更新时间:2023-11-30 11:21:24 28 4
gpt4 key购买 nike

如何让观察者进入监听者?示例:

ViewTreeObserver myObserver = myLayout.getViewTreeObserver();
myObserver.addOnPreDrawListener(new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
// I want to get the myLayout or myObserver here,
// but I don't want to use the lines below
RelativeLayout myLayout = (RelativeLayout)findViewById(myId);
myLayout.getViewTreeObserver().removeOnPreDrawListener(this);

最佳答案

我试过玩听众实例,我不知道它有多准确,但它有效:)

final class MyOnPreDrawListener implements OnPreDrawListener
{
private RelativeLayout myLayout;

public void setMyLayout(RelativeLayout layout){
myLayout = layout;
}

@Override
public boolean onPreDraw() {
// now I have myLayout here
return true;
}

}

然后我打电话

ViewTreeObserver observer = myLayout.getViewTreeObserver();
MyOnPreDrawListener myListener = new MyOnPreDrawListener();
myListener.setMyLayout(myLayout);
observer.addOnPreDrawListener(myListener);

关于java - 从附加的 OnPreDrawListener 对象中获取 ViewTreeObserver 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22248948/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com