java Android - 选项卡、MapView、选项卡内的活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1590340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Tabs, MapView, activities within tabs
提问by I82Much
We're in the process of writing an app that has 4 tabs: Map, People, Places, Events. The People, Places, and Events in the App show up as Icons on the map. By default the People, Places, and Events tabs each show a listview, custom rendered, displaying all the People, Places, and Events respectively.
我们正在编写具有 4 个选项卡的应用程序:地图、人物、地点、事件。应用程序中的人物、地点和事件在地图上显示为图标。默认情况下,人物、地点和事件选项卡各自显示一个列表视图,自定义呈现,分别显示所有人物、地点和事件。
alt text http://web6.twitpic.com/img/37202700-f92052dc474b74e1760edda1c47f6940.4adcb134-scaled.png
替代文字 http://web6.twitpic.com/img/37202700-f92052dc474b74e1760edda1c47f6940.4adcb134-scaled.png
Now, right now each of the tabs has as its content an Intent set to launch the corresponding activity. For instance, there is a MapTabActivity that extends MapActivity, a ShowPeopleListActivity that shows the people, and so on and so forth.
现在,现在每个选项卡都有一个 Intent 作为其内容来启动相应的活动。例如,有一个 MapTabActivity 扩展 MapActivity,一个 ShowPeopleListActivity 显示人员,等等。
I see a lotofStackOverflowquestions/answers saying that due to various limitations in the way the TabHost is setup, it's best NOT to use activities as the content of tabs. For instance, it's impossible to launch a new activity and have it take the place of the existing activity within a tab, whereas it's possible to switch out a View with a different view.
我看到了很多的StackOverflow的问题/回答说,由于路上的各种限制的TabHost是设置,最好不要使用活动选项卡的内容。例如,不可能启动一个新活动并让它代替选项卡中的现有活动,而可以切换出具有不同视图的视图。
Now, I'm at a crossroads. We've (for better or worse) devoted a fair amount of time trying to get this app to work the way it's currently structured, with the Activities as the content of the tabs. When an icon corresponding to a Person, Place, or Event is clicked, it fires off a VIEW Intent on a URI corresponding to that object; this is picked up by an Activity that then shows the object. The same mechanism is at work both in the Map and in the individual lists. We really like the loose coupling this provides us; we just give a VIEW command and the URI to the person/place/event and it automatically brings us to the right activity. Granted, the activity that's launched covers up the tab view rather than appearing inside of that, but we were willing to live with this.
现在,我正处于十字路口。我们已经(无论好坏)投入了相当多的时间,试图让这个应用程序按照当前的结构运行,将活动作为选项卡的内容。当点击与人物、地点或事件对应的图标时,它会在与该对象对应的 URI 上触发 VIEW Intent;这是由一个 Activity 拾取的,然后显示该对象。相同的机制在 Map 和单个列表中都起作用。我们真的很喜欢这为我们提供的松散耦合;我们只需向人/地点/事件提供 VIEW 命令和 URI,它就会自动将我们带到正确的活动。诚然,启动的活动覆盖了选项卡视图而不是出现在其中,但我们愿意接受这一点。
Here's an issue though: from the Show activity, we want to be able to go back to the map, centered at that person, place, or event. We can launch a new activity to show the map again, but now we have the map activity as the content of the tab, plus the show activity, plus the new map activity in the activity stack; given how resource intensive the map activity is, I'm guessing this is not the ideal way to go.
但这里有一个问题:从 Show 活动中,我们希望能够返回到地图,以那个人、地点或事件为中心。我们可以启动一个新的Activity来再次显示地图,但是现在我们有地图Activity作为tab的内容,加上show Activity,加上Activity栈中的新地图Activity;鉴于地图活动的资源密集程度,我猜这不是理想的方式。
I guess my question is, is there a GOOD tutorial somewhere showing exactly how to do complex tasks with a TabHost? I've seen HelloTabWidget; I'm looking for something much more sophisticated than this. I'm worried that if we switch to the View based way of doing things, we'll have to do a LOT of housekeeping to intercept all the back events, try to switch out the views, etc., etc., as well as strongly coupling our program in a way we don't want.
我想我的问题是,是否有一个很好的教程来展示如何使用 TabHost 执行复杂的任务?我见过HelloTabWidget;我正在寻找比这更复杂的东西。我担心如果我们切换到基于视图的做事方式,我们将不得不做很多内务来拦截所有后台事件,尝试切换视图等等,等等,以及以我们不想要的方式强耦合我们的程序。
Any suggestions on a way forward would be extremely appreciated. We're new to Android so we're trying to follow the established best practices, but it's difficult when the few examples we've seen are too simplistic for our use case.
任何关于前进道路的建议将不胜感激。我们是 Android 的新手,因此我们正在尝试遵循既定的最佳实践,但是当我们看到的少数示例对于我们的用例来说过于简单时,这很困难。
采纳答案by CommonsWare
I see a lot of StackOverflow questions/answers saying that due to various limitations in the way the TabHost is setup, it's best NOT to use activities as the content of tabs.
我看到很多 StackOverflow 问题/答案都说由于 TabHost 设置方式的各种限制,最好不要使用活动作为选项卡的内容。
As self-appointed President of the Anti-Activity-Tab Alliance (AATA), that's certainly my position.
作为反活动标签联盟 (AATA) 的自封主席,这当然是我的立场。
When an icon corresponding to a Person, Place, or Event is clicked, it fires off a VIEW Intent on a URI corresponding to that object; this is picked up by an Activity that then shows the object.
当点击与人物、地点或事件对应的图标时,它会在与该对象对应的 URI 上触发 VIEW Intent;这是由一个 Activity 拾取的,然后显示该对象。
Note that this has nothing to do with having activities as the contents of tabs.
请注意,这与将活动作为选项卡的内容无关。
We can launch a new activity to show the map again, but now we have the map activity as the content of the tab, plus the show activity, plus the new map activity in the activity stack; given how resource intensive the map activity is, I'm guessing this is not the ideal way to go.
我们可以启动一个新的Activity来再次显示地图,但是现在我们有地图Activity作为tab的内容,加上show Activity,加上Activity栈中的新地图Activity;鉴于地图活动的资源密集程度,我猜这不是理想的方式。
I'd avoid it if possible.
如果可能,我会避免它。
I'm worried that if we switch to the View based way of doing things, we'll have to do a LOT of housekeeping to intercept all the back events, try to switch out the views, etc., etc., as well as strongly coupling our program in a way we don't want.
我担心如果我们切换到基于视图的做事方式,我们将不得不做很多内务来拦截所有后台事件,尝试切换视图等等,等等,以及以我们不想要的方式强耦合我们的程序。
This doesn't follow at all from what you wrote previously. Your "back events" will not change one iota between using Views as the contents of tabs and using Activities as the contents of tabs. Furthermore, this has nothing whatsoever to do with the "loose coupling" pattern you describe -- clicking on an icon in a list in a view in a tab is no different than clicking on an icon in a list in a view in an activity in a tab.
这与您之前所写的完全不同。在使用视图作为选项卡的内容和使用活动作为选项卡的内容之间,您的“后台事件”不会改变一点点。此外,这与您描述的“松散耦合”模式没有任何关系——单击选项卡视图中列表中的图标与单击活动视图中列表中的图标没有什么不同一个选项卡。
Just have your Show activity tell your, um, main activity to show a particular location, then the Show activity can finish(). The simplest way to do that without introducing a hard JVM coupling between the activities is to broadcast an Intentand register a BroadcastReceiverin the main activity. Upon receipt of this Intent, the main activity would update the map and set it to be the current tab. Of course, this approach is simpler if you have the main activity use Views for its tab contents.
只需让您的 Show 活动告诉您的主要活动以显示特定位置,然后 Show 活动就可以了finish()。在不引入活动之间的硬 JVM 耦合的情况下,最简单的方法Intent是BroadcastReceiver在主活动中广播和注册 a 。收到此消息后Intent,主要活动将更新地图并将其设置为当前选项卡。当然,如果您的主要活动使用视图作为其选项卡内容,则这种方法会更简单。
Now, if you try to overhaul your application, such that navigating in a tab doesn't launch another activity, but rather keeps things within its own tab...that is a whole 'nuther kettle of fish.
现在,如果您尝试彻底检查您的应用程序,以便在选项卡中导航不会启动另一个活动,而是将内容保留在其自己的选项卡中......
回答by Josh Clemm
It's a good practice to have one activity and multiple views for your tabs. However, that does mean you have to be careful to handle which tab is selected, different menus and context menus for each tab view, etc.
为选项卡设置一个活动和多个视图是一种很好的做法。但是,这确实意味着您必须小心处理选择了哪个选项卡、每个选项卡视图的不同菜单和上下文菜单等。
I guess my question is, is there a GOOD tutorial somewhere showing exactly how to do complex tasks with a TabHost? I've seen HelloTabWidget; I'm looking for something much more sophisticated than this.
我想我的问题是,是否有一个很好的教程来展示如何使用 TabHost 执行复杂的任务?我见过HelloTabWidget;我正在寻找比这更复杂的东西。
I wrote a slightly better tutorial on my blog that demonstrates an interacting ListView and MapView as tabs. Here's the link: Android Tabs with interacting map and list views
我在我的博客上写了一个稍微好一点的教程,演示了一个交互的 ListView 和 MapView 作为选项卡。这是链接:具有交互地图和列表视图的 Android 选项卡
The basics is to have a layout similar to the one in the HelloTabWidget tutorial, make your activity extend from MapActivity, extract the tabhost from the XML and make sure you call setup() on the tabhost. After that, adding views as the content of tabs, tab listeners, etc. are the same.
基础是具有类似于 HelloTabWidget 教程中的布局,使您的活动从 MapActivity 扩展,从 XML 中提取 tabhost,并确保在 tabhost 上调用 setup()。之后,添加视图作为选项卡、选项卡侦听器等的内容是相同的。
Here's a brief starting point for the class:
以下是该课程的简要起点:
public class TabbedListMapActivity extends MapActivity {
private ListView listView;
private MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
// setup must be called if not a TabActivity
tabHost.setup();
// setup list view
listView = (ListView) findViewById(R.id.list);
// setup map view
mapView = (MapView) findViewById(R.id.mapview);
// add views to tab host
tabHost.addTab(tabHost.newTabSpec("List").setIndicator("List").setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView;
}
}));
tabHost.addTab(tabHost.newTabSpec("Map").setIndicator("Map").setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return mapView;
}
}));
}
...
...
回答by ericharlow
For instance, it's impossible to launch a new activity and have it take the place of the existing activity within a tab, whereas it's possible to switch out a View with a different view.
例如,不可能启动一个新活动并让它代替选项卡中的现有活动,而可以切换出具有不同视图的视图。
I don't think it is impossible. I experimented with this idea and wrote a blog articleexplaining what I did. I include a sample code project at the end of the article that demonstrates some of the interesting things I learned.
我不认为这是不可能的。我尝试了这个想法并写了一篇博客文章解释了我所做的。我在文章末尾包含了一个示例代码项目,它展示了我学到的一些有趣的东西。

