Android - 创建Generic TabHost,使用Bundle问题传递信息

时间:2011-01-13 14:08:49

标签: android android-tabhost

我有一个项目,我正在尝试对现有的TabHosts进行一些重构。我有一堆非常简单的TabHost文件,看起来像下面的类。有些实际上只有一个标签,有的只有3个等等 - 所以它们唯一真正的区别就是标签的数量和每个标签中加载的活动类。我想创建一个TabHost,它可以从传递的Bundle中获取信息,以确定构建/添加每个选项卡需要多少选项卡和所需信息(规范,指标,内容)。看起来我可以放在捆绑中的项目非常基础,我不熟悉Parcelable或Serializable功能。有什么建议吗?

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

1 个答案:

答案 0 :(得分:0)

看起来我过早地提出这个问题了。我最终使用Serializable类实现解决了我的问题。希望其他人认为这很有用。请参阅以下代码:

首先创建用于保存数据的类

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

然后更新了Generic Tab Host

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

此类的用户可以使用它:

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);