开发者

Android - creating a Generic TabHost, passing info using Bundle problem

I've got a project in which I'm trying to do some refactoring of my existing TabHosts. I've got a bunch of very simple TabHost files that look like the class below. Some actually only have one tab, some 3, etc - so the only real difference in them is the number of tabs and the activity class loaded in each one. I'd like to just create a single TabHost that could get the info out of a passed in Bundle to determine how many tabs and the info needed (spec, indicator, content) to build/add each tab. It seems like the items I can place开发者_开发技巧 in the bundle are pretty basic and I'm not familiar with the Parcelable or Serializable features. Any suggestions?

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


Looks like I jumped in asking this question too soon. I ended up solving my problem using a Serializable class implementation. Hopefuly somebody else finds this useful. See the code below:

First created class to hold the data

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

Then updated the 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

The user of this class can use it like:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜