2017-07-18 47 views
0

我正在使用ViewStub Android组件进行测试。我可以膨胀一个View并替换一个新的,但是当我执行一个新的替换现有的View,我怀疑这个驱逐舰从来没有在旧的替换Views中被调用过。这里是我的逻辑示例代码:Android ViewStub管理替换内部视图

protected void testInflateReplaceViewStub(){ 
    //case inflating a View... 
    View viewInflated = View.inflate(getContext(), R.layout.container_view, this); //container View 
    ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub_impl); //view inside layout 
    viewStub.setLayoutResource(R.layout.container_view); //fill with some content 
    View viewStubInflated = viewStub.inflate(); 
    //end inflating the first ViewStub 

    //case replacing above View by a new one... 
    viewStubInflated.invalidate(); //this should destroy old inner Views?? 
    viewInflated = View.inflate(getContext(), R.layout.container_view, this); 
    viewStub.setLayoutResource(R.layout.new_content);//fill new content 
    viewStubInflated = viewStub.inflate(); 
    //end replacing inflating 
} 

我想强制销毁所有旧的换成Views

回答

0

一个你膨胀消失层次的ViewStub,你不能再使用它:

布局资源的通货膨胀后“mySubTree”的ViewStub是 从其父中移除。

请参阅ViewStub documentation

尝试使用include甚至FrameLayout,取决于您的需求。