2012-08-14 111 views
3

我有一個tabhost和我添加有3活動(每片一個活性)。 我需要知道如何在每次更改標籤時在活動中調用新實習生。 我爲tabhost添加了一個監聽器。當我使用clearAllTabs();方法並將所有選項卡再次添加到偵聽器中時,應用程序崩潰。 當我使用代碼toto從視圖中刪除用戶單擊的特定選項卡tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(i)); tabHost.addTab(the tab I want to replace);刷新活動內部tabhost

然後新選項卡位於tabhost的末尾。

我只需要的是如何給每個用戶clickes特定的標籤頁時重新加載相稱活動的例子。

我的代碼:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // ActionBar bar = getSupportActionBar(); 

     // requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 

     Resources res = getResources(); 
     LocalActivityManager mlam = new LocalActivityManager(this, false); 
     final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
     mlam.dispatchCreate(savedInstanceState); 
     tabHost.setup(mlam); 
     TabHost.TabSpec spec; 
     Intent intent; 

     // TabHost tabHost = getTabHost(); 
     // tabHost.setup(); 

     TabSpec specAll = tabHost.newTabSpec("All"); 
     specAll.setIndicator("All"); 
     Intent allIntent = new Intent(this, allActivity.class); 
     specAll.setContent(allIntent); 

     // specAll.setContent(R.id.allList); 

     Log.d("SpecAll",""+specAll.setContent(allIntent)); 

     TabSpec specIn = tabHost.newTabSpec("in"); 
     specIn.setIndicator("In"); 
     Intent inIntent = new Intent(this, inActivity.class); 
     specIn.setContent(inIntent); 

     TabSpec specOut = tabHost.newTabSpec("Out"); 
     specOut.setIndicator("Out"); 
     Intent outIntent = new Intent(this, outActivity.class); 
     specOut.setContent(outIntent); 

     // Adding all TabSpec to TabHost 
     tabHost.addTab(specAll); // Adding all tab 
     tabHost.addTab(specIn); // Adding in tab 
     tabHost.addTab(specOut); // Adding out tab 



     tabHost.setOnTabChangedListener(new OnTabChangeListener() { 



         @Override 
         public void onTabChanged(String tabId) { 

          int i = tabHost.getCurrentTab(); 
          //Log.i("@@@@@@@@ ANN CLICK TAB NUMBER", "------" + i); 

          if (i == 0) { 
           Log.d("TAB","" +i); 

          } else if (i == 1) { 
           Log.d("TAB","" +i); 
          } 
          else 
           Log.d("TAB", ""+i); 
         } 
        }); 


    } 
+0

我也遇到這個問題,雖然略有不同:我想在一個標籤中替換活動。我沒有找到解決方案,但保存所有標籤內容,關閉TabActivity,立即重新打開並恢復內容。我相信有更好的解決方案。 – FWeigl 2012-08-14 09:12:05

+0

我發佈瞭解決方案隊友。歡呼聲 – mkounal 2012-08-14 15:23:04

回答

4

似乎存在與活動,tabhost.in爲了重新加載活動無論你只需要做:

specAll.setContent(yourIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 

就在tabHost.addTab前

0

我也面臨同樣的問題,但我像這樣解決這個問題...

這是我TabActivity ....

public class MainActivity extends TabActivity { 
TabHost tabhost; 
String cTab = "0"; 
String nTab; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 
    tabhost = getTabHost(); 

    TabSpec one = tabhost.newTabSpec("0"); 

    // setting Title and Icon for the Tab 
    one.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher)); 
    Intent songsIntent = new Intent(this, FirstActivity.class); 
    one.setContent(songsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
    one.setContent(songsIntent); 

    TabSpec two = tabhost.newTabSpec("1"); 
    // setting Title and Icon for the Tab 
    two.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher)); 
    Intent songsIntent1 = new Intent(this, SecondActivity.class); 
    two.setContent(songsIntent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
    two.setContent(songsIntent1); 

    TabSpec three = tabhost.newTabSpec("2"); 
    // setting Title and Icon for the Tab 

    three.setIndicator("", 
      getResources().getDrawable(R.drawable.ic_launcher)); 
    Intent songsIntent4 = new Intent(this, ThirdActivity.class); 
    three.setContent(songsIntent4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
    three.setContent(songsIntent4); 

    TabSpec four = tabhost.newTabSpec("3"); 
    // setting Title and Icon for the Tab 
    four.setIndicator("", getResources() 
      .getDrawable(R.drawable.ic_launcher)); 
    Intent songsIntent5 = new Intent(this, FourthActivity.class); 
    four.setContent(songsIntent5.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
    four.setContent(songsIntent5); 

    tabhost.addTab(one); 
    tabhost.addTab(two); 
    tabhost.addTab(three); 
    tabhost.addTab(four); 
    tabhost.setOnTabChangedListener(new OnTabChangeListener() { 
     @Override 
     public void onTabChanged(String arg0) { 
      cTab = "" + tabhost.getCurrentTab(); 

     } 
    }); 

    int numberOfTabs = tabhost.getTabWidget().getChildCount(); 
    for (int t = 0; t < numberOfTabs; t++) { 
     tabhost.getTabWidget().getChildAt(t) 
       .setOnTouchListener(new View.OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         if (event.getAction() == MotionEvent.ACTION_UP) { 
          String currentSelectedTag = MainActivity.this 
            .getTabHost().getCurrentTabTag(); 
          nTab = currentSelectedTag; 
          System.out.println(" nTab " + nTab); 
          System.out.println(" cTab " + cTab); 
          if (cTab.equals(nTab)) { 
           if (nTab.equals("0")) { 
            Intent intent = new Intent(); 
            intent.setAction("com.ensis.first"); 
            MainActivity.this.sendBroadcast(intent); 
           } 
           if (nTab.equals("1")) { 
            Intent intent = new Intent(); 
            intent.setAction("com.ensis.second"); 
            MainActivity.this.sendBroadcast(intent); 
           } 
           if (nTab.equals("2")) { 
            Intent intent = new Intent(); 
            intent.setAction("com.ensis.third"); 
            MainActivity.this.sendBroadcast(intent); 
           } 
           if (nTab.equals("3")) { 
            Intent intent = new Intent(); 
            intent.setAction("com.ensis.fourth"); 
            MainActivity.this.sendBroadcast(intent); 
           } 
          } 
         } 
         return false; 
        } 
       }); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
}} 

這是我FirstActivity.java

public class FirstActivity extends ActivityGroup{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onCreate(savedInstanceState); 

ok(); 
IntentFilter filter = new IntentFilter("com.ensis.first"); 
registerReceiver(myReceiver, filter); 

/**/ 
    } 

    private void ok() { 
// TODO Auto-generated method stub 
setContentView(R.layout.firstscreen); 
Button bt=(Button)findViewById(R.id.button1); 
bt.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Intent it=new Intent(FirstActivity.this,SubActivity.class); 
     replaceContentView("activity3", it); 
    } 
    }); 
     } 

      public void replaceContentView(String id, Intent newIntent) { 
     View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); 
     this.setContentView(view); 
    } 

    private BroadcastReceiver myReceiver = new BroadcastReceiver() 
    { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.v("22222222222222222", "22222222222"); 
     ok(); 
    } 
     };} 
3

在我的簡單的情況下,當我不需要堅持我標籤片段使用下面的代碼

 int currentTabId = mTabHost.getCurrentTab(); 
    mTabHost.clearAllTabs(); 
    setupTabs(); 
    mTabHost.setCurrentTab(currentTabId); 
+0

樣的解決方案..... – 2014-09-02 10:46:49

+0

你救了我的時間......愛你<3 – 2015-01-17 12:16:29