Android ViewStub管理替换内部视图

时间:2017-07-18 09:42:53

标签: android android-inflate viewstub

我正在使用ViewStub Android组件进行测试。我可以膨胀一个View并替换一个新的,但当我用新的替换现有的View时,我怀疑驱逐舰永远不会被旧的替换视图调用。这是我逻辑的示例代码:

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

1 个答案:

答案 0 :(得分:0)

你膨胀ViewStub它会消失层次结构,你不能再次使用它了:

  

布局资源" mySubTree," ViewStub是   从父母那里删除。

请参阅ViewStub documentation

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