2011-10-02 245 views
0

如果我想显示MapViewActivity我会到我的ClassMapActivity延长内部。如果我想显示Tabbed界面我会到我的ClassTabActivity延伸。其他一些需要用户类扩展特定类的控件的情况也是如此。最佳实践?

假设在我的Activity里面我想同时显示用于显示Google Map的MapView和显示其他一些数据的TabView。我不能直接这样做,因为Java不支持多重继承。我的问题是我如何实现这种情况?如何在我的活动中显示多个控件,每个控件都需要您的类从特定的类扩展。首先可能吗?如果是,那么实现这种情况的最佳做法是什么?

更新我想要实现这个

enter image description here

我使用的地图和Tab为例的缘故。我想知道你通常可以如何处理这种情况?

回答

0

你可以通过对象组合生成它。起初我不知道如何让活动时间,并将其添加到布局,但后来我发现了LocalActivityManager,让你可以嵌入其他活动为您的视图。请注意,由于API级别11.在这里任何情况下,此类已废弃的嵌入其他活动需要扩展为View步骤:

  • 创建LocalActivityManager,使活动范围内活动的创建
  • 启动要嵌入并获得通过getDecorView()
  • 查看添加在布局

下查看活动是我的测试代码,我在我的活动范围内尝试


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Create local activity manager so that I could start my activity 
    LocalActivityManager localActivityManager = new LocalActivityManager(this, true); 
    // dispatch the onCreate from this manager 
    localActivityManager.dispatchCreate(savedInstanceState); 

    // layout to hold the activity, optionally this could be set through XML file 
    LinearLayout layout = new LinearLayout(this); 
    layout.setOrientation(LinearLayout.VERTICAL); 
    this.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
      ViewGroup.LayoutParams.FILL_PARENT)); 

    // start the activity which is in this example is an extension of a TabActivity 
    Intent tabIntent = new Intent(this, DummyTabActivity.class); 
    Window tabWindow = localActivityManager.startActivity("tabView", tabIntent); 
    View tabView = tabWindow.getDecorView(); 

    // start the activity that extends MapActivity 
    Intent mapIntent = new Intent(this, DummyMapView.class); 
    Window mapViewWindow = localActivityManager.startActivity("mapView", mapIntent); 
    View mapView = mapViewWindow.getDecorView(); 

    // dispatch resume to the Activities 
    localActivityManager.dispatchResume(); 

    // add to the tabView, optionally you could use other layout as well 
    layout.addView(tabView); 
    // add to the mapView, optionally you could use other layout as well 
    layout.addView(mapView); 
} 

我的有限实验表明,通过上述方法的对象组合将实现您正在尝试做的事情。话虽如此,我不确定这种方法有多普遍。没有你的问题,我可能不会去寻找上述方法,但你的用例很有趣,可能适用于将来的使用。我会研究片段,看看我是否可以对它做同样的事情,并在适用的情况下更新我的答案。

+0

你没有明白。我的问题是如何使用需要扩展课程的两个“并排”控制器。我知道我可以把MapView里面的TabView“如果”我想我的MapView“内部”MapView –

+0

你是对的,我不明白这个问题(与刚刚回答的其他人一样)。也许你可以澄清这个问题?你问这个问题的方式似乎在问如何一起使用TabView和MapView。根据你的评论,我猜你是在问如何使用多个控件,而这些控件依赖于扩展其他类? – momo

+0

我刚刚看到你的图:),现在事情更清晰。看起来不像有明显的方法来做到这一点(在Android中)。让我检查并回复你。 – momo

0

在这种情况下很简单:您可以使用TabActivity中的MapActivity(它旨在将活动作为选项卡进行管理)。

作为一般方法,我总是喜欢使用视图并将它们嵌套在一个活动中。我从来没有使用像ListActivity这样的东西。他们应该让事情变得简单,但对我来说往往看起来像一个糟糕的设计决定我从来没有遇到过的事实,我有两个活动(预计TabActivity)相结合。你可以看看this question。看来,这些活动从来不打算以这种方式使用。我想你描述的情况是为什么fragments在哪里介绍的。