【问题标题】:Best practise for displaying multiple controls which require an extended class?显示需要扩展类的多个控件的最佳实践?
【发布时间】:2011-10-02 18:50:42
【问题描述】:

如果我想在我的Activity 中显示MapView,我必须从MapActivity 扩展我的Class。如果我想显示Tabbed 界面,我必须从TabActivity 扩展我的Class。其他一些需要用户类从特定类扩展的控件也是如此。

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

更新我想实现这个

为了举例,我使用 Map 和 Tab。我想知道您一般如何处理这种情况?

【问题讨论】:

    标签: java android


    【解决方案1】:

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

    作为一般方法,我总是更喜欢使用视图并将它们嵌套在活动中。我从不使用像ListActivity 这样的东西。他们应该让事情变得更容易,但在我看来,这通常是一个糟糕的设计决定。我从来没有面对过我必须结合两个活动的事实(期待TabActivity)。

    你可以看看this question。似乎活动从来不打算以这种方式使用。我认为您描述的情况是引入fragments的原因。

    【讨论】:

      【解决方案2】:

      您可以通过对象组合来构建它。最初我不确定如何启动 Activity 并将其添加到布局中,但后来我发现了 LocalActivityManager,它允许您将其他 Activity 作为视图嵌入。请注意,此类自 API 级别 11 起已弃用。无论如何,这里是嵌入其他需要扩展为 View 的 Activity 的步骤:

      • 创建一个LocalActivityManager 以允许在 Activity 中创建 Activity
      • 启动要嵌入的活动并通过getDecorView() 获取视图
      • 在布局中添加视图

      以下是我在 Activity 中尝试的测试代码

      
      @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);
      }
      

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

      【讨论】:

      • 你没有明白这一点。我的问题是如何使用两个需要扩展类的“​​并排”控件。我知道我可以将 MapView 放在 TabView "IF" 我想要我的 MapView "Inside" MapView
      • 你说得对,我没听懂这个问题(和刚刚回答的其他人一样)。也许你可以澄清这个问题?您提出问题的方式似乎是在询问如何一起使用 TabView 和 MapView。根据您的评论,我猜您是在问如何使用多个依赖于扩展其他类的控件?
      • 我刚看到你的图表 :) 现在事情更清楚了。看起来没有明显的方法(在Android中)。让我检查一下并回复你。
      • @HarisHasan 我已经更新了答案以更好地反映问题:) 基本上我使用对象组合(这是常见的)来包含视图,唯一的事情是我不知道有多常见方法是使用 LocalActivityManager。我还没有想出从另一个 Activity 中启动 Activity 的任何其他方法。我将查看新的 Fragment API,看看是否可以用它更新我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      相关资源
      最近更新 更多