2011-01-13 6 views
0

私は既存のTabHostsのリファクタリングを実行しようとしているプロジェクトがあります。私は以下のような非常にシンプルなTabHostファイルをたくさん持っています。実際には1つのタブしか持たないものもあれば、3つのものもあります。実際には唯一の違いは、タブの数とそれぞれにロードされるアクティビティクラスです。私はちょうどいくつかのタブと情報(スペック、インジケータ、コンテンツ)を構築/各タブを追加するためにいくつかの情報を決定するために渡された情報を得ることができる単一のTabHostを作成したいと思います。私はバンドルに置くことができる項目はかなり基本的で、私はParcelableまたはSerializableの機能に慣れていないようです。助言がありますか?Android - バンドルの問題を使用して情報を渡す、一般的なTabHostを作成する

public class SomeTabHost 
     extends ActivityGroup 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     // Inflate ourselves into the layout ViewStub 
     ViewStub vs = (ViewStub) findViewById(R.id.theViewStub); 
     vs.setLayoutResource(R.layout.my_tabhost); 
     vs.inflate(); 

     TabHost host = (TabHost) findViewById(android.R.id.tabhost); 
     host.setup(getLocalActivityManager()); 

     host.addTab(host.newTabSpec("Tab1") 
         .setIndicator("Tab1") 
         .setContent(new Intent(this, SomeActivity.class))); 

     host.addTab(host.newTabSpec("Tab2") 
         .setIndicator("Tab2") 
         .setContent(new Intent(this, SomeOtherActivity.class))); 

     int numChildren = host.getTabWidget().getChildCount(); 
     for (int i=0; i <numChildren; i++) 
     { 
     host.getTabWidget().getChildAt(i).getLayoutParams().height = 35; 
     } 
    }// end onCreate 
}// end class 

答えて

0

私はこの質問をあまりにも早く求めているように見えます。私はSerializableクラスの実装を使用して私の問題を解決しました。 Hopefuly他の誰かがこれを見つけるのは便利です。

ArrayList<TabDetails> tabDetailsArray = new ArrayList<TabDetails>(2); 
tabDetailsArray.add(new TabDetails("Tab_1", 
            "Tab 1", 
            SomeActivity.class)); 
tabDetailsArray.add(new TabDetails("Tab_2", 
            "Tab 2", 
            AnotherActivity.class)); 

Intent intent = new Intent(getApplicationContext(), GenericTabHost.class); 
intent.putExtra(GenericTabHost.TABS, tabDetailsArray); 
startActivity(intent); 

まず作成したクラス

public class TabDetails implements Serializable 
{ 
    private static final long serialVersionUID = 1L; 

    public String tabSpec  = ""; 
    public String tabIndicator = ""; 
    public Class<?> tabContent = null; 

    public TabDetails(String aTabSpec, 
         String aTabIndicator, 
         Class<?> aTabContent) 
    { 
     this.tabSpec  = aTabSpec; 
     this.tabIndicator = aTabIndicator; 
     this.tabContent = aTabContent; 
    } 
}//end class 

はその後、このクラスのユーザーはそれが好きで使用することができます

public class GenericTabHost extends ActivityGroup 
{ 
    public static final String TABS = "TABS"; 

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

     // Inflate ourselves into the layout ViewStub 
     ViewStub vs = (ViewStub) findViewById(R.id.theViewStub); 
     vs.setLayoutResource(R.layout.mc_tabhost); 
     vs.inflate();  

     TabHost host = (TabHost) findViewById(android.R.id.tabhost); 
     host.setup(getLocalActivityManager()); 

     Bundle bundle = this.getIntent().getExtras(); 

     if (null != bundle) 
     { 
     ArrayList<TabDetails> tabDetailsList = (ArrayList<TabDetails>) bundle.getSerializable(GenericTabHost.TABS); 

     for (TabDetails tabDetails : tabDetailsList) 
     { 
      host.addTab(host.newTabSpec (tabDetails.tabSpec ) 
          .setIndicator(tabDetails.tabIndicator) 
          .setContent (new Intent(getApplicationContext(), 
                 tabDetails.tabContent )); 
      } 

      int numChildren = host.getTabWidget().getChildCount(); 
      for (int i=0; i <numChildren; i++) 
      { 
       host.getTabWidget().getChildAt(i).getLayoutParams().height = 35; 
      } 
     } 
     } 
     else 
     { 
     Log.e("GenericTabHost", "#### This class must be passed in data to build itself ####"); 
     } 

    }// end onCreate 

}// end class 

一般]タブのホストに更新されたデータを保持するために:以下のコードを参照してください。

関連する問題