2013-09-26 110 views
2

我想在FragmentTabHost與片段溝通的FragmentTabHost

片段 - >活動完成通信與片段溝通!帶有一個接口。

但我不能創造一個溝通活動 - >碎片,因爲我創造了這樣的片段:

mTabHost.addTab(
      mTabHost.newTabSpec("tab2").setIndicator("Affichage", 
        getResources().getDrawable(android.R.drawable.star_on)), 
      MySelectionFragment.class, null); 

MySelectionFragment是一類不喜歡new MySelectionFragment()

一個片段,我不知道如何溝通帶班:/

在此先感謝!

+0

我不能找到在['TabHost']的任何方法(http://developer.android.com/reference/android/widget /TabHost.html)作爲'addTab(TabSpec,Class,????)'。你說你用了什麼方法? – gunar

+0

我也沒有。我在答案中發現它:http://stackoverflow.com/questions/17227855/tabhost-with-fragments-and-fragmentactivity – nsvir

+0

...你實際上使用了'FragmentTabHost'。當您發佈問題時請詳細說明。不要把人引向另一個方向。 – gunar

回答

2

訣竅是重寫onAttach方法是這樣的:

@Override 
public void onAttachFragment(android.support.v4.app.Fragment attachedFragment) { 
    super.onAttachFragment(attachedFragment); 

    if (attachedFragment.getClass().equals((ObjectA.class)) { 
     mObjectA = (ObjectA)attachedFragment; 
    } 
    if (attachedFragment.getClass().equals((ObjectB.class)) { 
     mObjectB = (ObjectB) attachedFragment; 
    } 
}