Android - onGlobalLayout未被ViewTreeObserver

时间:2016-12-01 18:59:26

标签: android android-canvas android-view android-viewtreeobserver

我在当前的Android项目中头疼。我想检测当前页面的更改。例如,有一个 TextView 来显示设备时间,每秒更新一次。如何检测这种变化?我在SO上搜索了很多(感谢SO),但没有一个对我有用。

更多信息:我没有使用标准活动来创建页面。我的方式是: 所有窗口小部件都创建为View对象,然后用于创建容器对象。之后,我只是处理这个容器,在画布上定期绘制 VSYNC 回调 Choreographer.FrameCallback

确实,它可以吸引人。一切都好。除外:我只想在页面内容发生变化时绘制画布。回到我开始的问题,如何检测这个"改变"事件?我确信有一些回调来处理这个问题。我使用了以下解决方案,但是当textview的文本发生更改时,不会调用 onGlobalLayout

CanvasAppViewContainer container;//CanvasAppViewContainer extends AbsoluteLayout
LayoutInflater li =(LayoutInflater)getService().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    

View view = li.inflate(resourceId, null);//passed a correct the layout id
container = new CanvasAppViewContainer(getService(), view, getWidth(), getHeight(), getSurface());
rootView = (LinearLayout) findViewById(R.id.rootView); //root element of layout
rootView.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
    @SuppressLint("NewApi")
    @Override
    public void onGlobalLayout() {
        Log.d("test", "onGlobalLayout");
    }
});

BTW:即使我为textview注册了视图树观察者,仍然没有调用onGlobalLayout。

由于

1 个答案:

答案 0 :(得分:1)

onGlobalLayout()仅在布局更改时调用。如果要检测文本中的更改,请使用TextView.addTextChangedListener()