最近做完了一个客户端项目
外面是个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.
-
TabHost tabHost = getTabHost();
-
-
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Player").setContent(
-
new Intent(this, PlayerActivity.class)));
-
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Archive").setContent(
-
new Intent(this, ArchiveGroup.class)));
-
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Updates").setContent(
-
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.
-
View view = getLocalActivityManager().startActivity("ArchiveActivity",
-
new Intent(this, ArchiveActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
-
-
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.
-
String album = (String) getListView().getItemAtPosition(position);
-
Intent intent = new Intent(getApplicationContext(), ArchiveAlbums.class);
-
intent.putExtra("album", album);
-
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-
-
View view = ArchiveGroup.group.getLocalActivityManager().startActivity("ShowPodcasts", intent).getDecorView();
-
-
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.
分享到:
相关推荐
有时需要每个tab中有多个页面,也就是说需要 在一个tab中跳转不同的Activity。 当时,我只是简单的在一个Activity中使用了动态布局 (设置layout是否显示) 虽然效果还不错,但实际上依然是同一个Activity,这样的...
1:解决了tabhost 位于底部的问题; 2:解决了tabhost 一个tab页中多个activity的跳转及返回问题。
自定义TabHost放在Activity底部,实现多个Activity的切换!
TabHost 同一个Tab内Activity之间的跳转的实现
点击TabHost的tab页,切换后更换tab背景颜色
Tabwidget的不同tab指向不同的Activity
用ActivityGroup解决TabHost中多个Activity跳转问题 参见http://hkp2008.iteye.com/blog/1185482
简单的android选项卡Tab菜单TabHost(一)同一Activity上实现
仿微信例子,使用TabHost实现底部tab,可以显示未读信息条数。适合初学者学习研究。
几种tab切换模式,tabhost模式代码分享
TabHost内实现多个Activity的跳转
Android TabHost实现屏幕UI窗口的TAB切换功能,实现的过程:声明TabHost对象、取得TabHost对象,为TabHost添加标签,新建一个newTabSpec(newTabSpec),设置其标签和图标(setIndicator),设置内容(setContent),然后...
Tab与TabHost 这就是Tab,而盛放Tab的容器就是TabHost 如何实现?? 每一个Tab还对应了一个布局,这个就有点好玩了。一个Activity,对应了多个功能布局
自定义tabhost实现activity 的跳转 界面比较简单 但功能都实现了
简单的实现了TabHost动态生成TAB选项卡,单独引用头部尾部信息
TabHost中填充自定义ListView
解决tabhost子activity不响应OnActivityResult()函数
主界面tabhost+activitygroup框架
仿新浪微博Android例子源码tabhost+activity+slidingMenu
tabhost中嵌套一个tabhost。