【so easy】Tabhost每个tab中放入多个Activity!

最近做完了一个客户端项目

外面是个tabhost(用于底部tab栏)每个tab中有一个Activity,这个很普遍。

但有时需要每个tab中有多个页面,也就是说需要 在一个tab中跳转不同的Activity。

当时,我只是简单的在一个Activity中使用了动态布局 (设置layout是否显示)

虽然效果还不错,但实际上依然是同一个Activity,这样的缺点是代码结构会相对复杂,不易维护

今天在网上看到了一篇文章,原文是http://united-coders.com网站的http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity

特意留下来 供大家和自己以后使用:

 

 

Use Android ActivityGroup within TabHost to show different Activity

 

 

For that reason you need a ActivityGroup within the Tab where you want to change the Activity.
An ActivityGroup is: A screen that contains and runs multiple embedded activities.

Let's look at this at a real life example. An Android app that shows your podcasts. In the first Activity you get to see all podcasts by subscription. If you touch the subscription, you see the single podcasts you've downloaded for that subscription.

The Tabhost contains three tabs, one for the MediaPlayer, one for the archive and one for available downloads.

  1.         TabHost tabHost  = getTabHost ( ) ;
  2.  
  3.         tabHost. addTab (tabHost. newTabSpec ( "tab1" ). setIndicator ( "Player" ). setContent (
  4.                  new Intent ( this, PlayerActivity. class ) ) ) ;
  5.         tabHost. addTab (tabHost. newTabSpec ( "tab2" ). setIndicator ( "Archive" ). setContent (
  6.                  new Intent ( this, ArchiveGroup. class ) ) ) ;
  7.         tabHost. addTab (tabHost. newTabSpec ( "tab3" ). setIndicator ( "Updates" ). setContent (
  8.                  new Intent ( this, DownloadList. class ) ) ) ;

 

The ArchiveGroup takes care which Activity is shown in the second tab. With setContentView you can bring the View to the front.

  1.          View view  = getLocalActivityManager ( ). startActivity ( "ArchiveActivity",
  2.                  new Intent ( this, ArchiveActivity. class ). addFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP ) ). getDecorView ( ) ;
  3.        
  4.         setContentView (view ) ;

 

Now all you need to do is to bring another View to the front after an action is triggered. In this case, after a ListItem is clicked.

  1.          String album  =  ( String ) getListView ( ). getItemAtPosition (position ) ;
  2.         Intent intent  =  new Intent (getApplicationContext ( ), ArchiveAlbums. class ) ;
  3.         intent. putExtra ( "album", album ) ;
  4.         intent. addFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP ) ;
  5.        
  6.          View view  = ArchiveGroup. group. getLocalActivityManager ( ). startActivity ( "ShowPodcasts", intent ). getDecorView ( ) ;
  7.        
  8.         ArchiveGroup. group. setContentView (view ) ;


You need to tell the ActivityGroup which view is to be on top and set the appropriate Flag, so that the View will be on top.

 

Of course now you have to keep track which activity is in front, how they behave and what happens if the back button is pressed. But that gives you room for customizing the behavior.

The full code can be found on github in the GpodRoid project.

Another good tutorials are by H. Larsen and Eric Harlow.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面我将为你提供一个简单的示例代码,演示如何在Android Studio动态添加项目的TabHost。 1. 首先,在XML布局文件添加TabHost组件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </TabHost> ``` 2. 在Java代码找到对应的TabHost组件并初始化: ```java TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 创建一个新的TabSpec对象,用于描述每个标签页: ```java TabHost.TabSpec spec; spec = tabHost.newTabSpec("tag1"); spec.setIndicator("Tab1"); spec.setContent(R.id.tab1); tabHost.addTab(spec); ``` 4. 重复步骤3,以添加其他标签页: ```java spec = tabHost.newTabSpec("tag2"); spec.setIndicator("Tab2"); spec.setContent(R.id.tab2); tabHost.addTab(spec); spec = tabHost.newTabSpec("tag3"); spec.setIndicator("Tab3"); spec.setContent(R.id.tab3); tabHost.addTab(spec); ``` 5. 最后,你可以在代码动态添加标签页: ```java TabHost.TabSpec spec = tabHost.newTabSpec("tag4"); spec.setIndicator("Tab4"); spec.setContent(new TabHost.TabContentFactory() { @Override public View createTabContent(String tag) { TextView textView = new TextView(MainActivity.this); textView.setText("This is Tab 4"); return textView; } }); tabHost.addTab(spec); ``` 这个示例演示了如何在Android Studio动态添加项目的TabHost。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值