【问题标题】:Android: TabActivity which performs a check before populating the content viewAndroid:TabActivity 在填充内容视图之前执行检查
【发布时间】:2011-02-04 01:08:09
【问题描述】:

我有一个 TabActivity 类,它使用 Intents 填充内容视图。在某些情况下,我想拦截一个选项卡选择事件,弹出一个消息对话框,抑制选择的 Intent,并恢复到原来选择的选项卡。

我希望 TabActivity 内容保持 Intent 驱动(而不是使用视图)。

我怀疑这可能需要扩展 LocalActivityManager。

有没有人做过这个或做过类似的事情?

// simple example of current code:

TabHost tabHost = getTabHost();
TabSpec ts = tabHost.newTabSpec(tag);
ts.setIndicator(tabview);
ts.setContent(new Intent().setClass(this, AHome.class));
tabHost.addTab(ts);

谢谢!

【问题讨论】:

  • “我希望 TabActivity 内容保持 Intent 驱动(而不是使用视图)”——为什么?
  • @CommonsWare:因为我希望内容视图包含一个活动而不是一个视图。活动已经针对其内容进行了构建。它们本质上是 MVC 中的控制器。
  • “活动已经针对其内容构建。它们本质上是 MVC 中的控制器。” -- MVC 在 Android 发明之前就已经存在。您甚至可以在 Wikipedia 中查看这些技术的日期。因此,根据定义,可以在没有活动的情况下进行 MVC。您使用这些称为“类”的神奇事物。相反,您将浪费堆栈空间、堆空间、CPU 时间和电池寿命,只是为了以活动的形式组织代码。
  • @CommonsWare:我很清楚 MVC 早于 Android 并且活动比简单的自定义类更重。假设我出于多种原因之一将它们作为活动(例如,将它们发布以在另一个应用程序的 Activity 堆栈上使用,或者我希望另一个应用程序的 Activity 在我自己的堆栈上使用)......但这不是重点。

标签: android android-tabhost tabactivity


【解决方案1】:

我不会在 TabActivity 中寻找答案(甚至 Google 员工都承认这个 API 已损坏)。 这就是我所做的 - 在目标活动中,我会在 onCreate 中检查这个条件,如果满足条件,则继续,如果不满足 - 激活上一个活动

【讨论】:

  • 谢谢。请您告诉我您将如何“激活上一个活动”,并这样做以便重新选择上一个选项卡?
【解决方案2】:

在深入研究了 Android 的 TabHost src 之后,这里有一个相当简单的问题解决方案。它允许以图形方式“触摸”选项卡按钮但仍保持未选中状态,并阻止对选定选项卡的任何处理(假设所有 OnTabSelected 侦听器都已知晓)。

只需扩展 TabHost 类:

public class MyTabHost extends TabHost
{
    public MyTabHost(Context context)
    {
        super(context);
    }

    public MyTabHost(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public void setCurrentTab(int index) 
    {
        // e.g. substitute ? with the tab index(s) for which to perform a check.  
        if (index == ?)
        {
            if (/* a block condition exists */)
            {
                // Perform any pre-checking before allowing final tab selection
                Toast.makeText(this.getContext(), "msg", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        super.setCurrentTab(index);
    }
}

然后在用于 TabActivity 的 XML 中将您的引用从 TabHost 更改为 MyTabHost

<com.hos.MyTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <LinearLayout    
        android:id="@+id/llTest"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="0dp"
        >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="0dp" 
        android:layout_gravity="top"
        android:layout_weight="1"
        />

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"            
        android:layout_weight="0"
        />

    </LinearLayout>

</com.hos.MyTabHost>

要记住的另一件事是,如果您在 TabActivity 中使用 TabActivity.getTabHost(),它将返回一个 MyTabHost。例如:

MyTabHost mth = (MyTabHost)getTabHost();

【讨论】:

    猜你喜欢
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2019-11-05
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多