2011-07-12 31 views
1

我有一個tabhost。其中一個選項卡的活動是ViewGroup。該視圖組管理兩個不同的活動。我這樣做是爲了讓我可以在選項卡中的活動之間導航。我添加如下活動:Android ViewGroup移除視圖?

if (videoViewLive == null) 
      videoViewLive = getLocalActivityManager().startActivity("VideoPlayerLive", new Intent(this,VideoPlayerLive.class). 
        addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 

     videoViewLive.setVisibility(View.VISIBLE); 
     this.addContentView(videoViewLive, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT)); 

我的每個內容視圖活動都會收到異步通知。我想要做的是以某種方式刪除未被使用的活動/內容視圖。因此,本質上,我加載內容視圖A,B死亡,變爲空,或其他任何,反之亦然。我想這樣做,因爲我管理這些視圖的方式似乎有問題。 (加載視圖時出錯,加載其他視圖,然後再次加載第一個視圖等)

回答

0

這與這個問題有點相似,但你爲什麼要用單獨的活動來做這件事?

這正是Fragment的設計目的。實際上,android support library中包含ViewPager類別(另請參閱FragmentStatePagerAdapter),該類別允許通過選項卡(可能位於ActionBar)中的相同類型的行爲或滑動。適配器自動處理Fragment的生命週期,在它們之間導航時,全都在單個Activity的上下文中,因此您可以使用頂級Activity來路由事件並在必要時維護總體狀態。

0

我會嘗試以下方法:

//Add OnGlobalLayout Listener using ViewTreeObserver 

View rootView = (android.R.id.content); 

//Assuming you are managing these two activities inside the ViewGroup 
Activity activityA = <someRef Value>; 
Activity activityB = <someRef Value>; 

rootView.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){ 
    //try couple of things here 
    // 1. determine which activity has focus 
    // 2. you could also check position of View on screen to determine which one is active or on the top 
    if (activityA.hasWindowFocus()) 
    { 
     //do some action --remove other content from ViewGroup 
    } 
});