2017-07-18 131 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,取決於您的需求。