如何在TabHost中获取Tab的内容?

问题描述 投票:9回答:4

我有一个Activity定义为:

<LinearLayout
        android:id="@+id/TabContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="99"
        android:orientation="vertical">
  <TabHost
        android:id="@+android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/TabLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
      <TabWidget
        android:id="@+android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></TabWidget>
      <FrameLayout
        android:id="@+android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"></FrameLayout>
    </LinearLayout>
  </TabHost>
</LinearLayout>

基本上它是一个带有TextViews作为内容的TabHost。我正在使用createTabContent()动态构建这些TextViews的选项卡和内容。这很好。但在某些情况下(某些事件由带有TCP套接字的AsyncTask处理),我需要更改其中一些TextView的内容。

我确信我的问题是我对Android SDK缺乏经验的结果,但我找不到迭代TabHost并获取每个TextView的内容的方法。

我所取得的“最接近”的方法是:

TabHost th = (TabHost) findViewById(android.R.id.tabhost);
TabWidget tw = th.getTabWidget();    

for (int i = 0; i < tw.getChildCount(); i++) {
  LinearLayout lLayout = (LinearLayout) tw.getChildAt(i);
  TextView tv = ((TextView) lLayout.getChildAt(i));      

  tv.append(linea);
}

但这样做是将“linea”附加到选项卡的名称(指示符)。我想将它添加到该选项卡的内容,但我找不到一种方法来检索与之关联的TextView。

任何想法将非常感谢!

---------编辑---------

虽然在下面的评论中我发布了我已经有解决方案,但我发现它无效。代码现在看起来像这样:

for (int i = 0; i < tw.getChildCount(); i++) {
  LinearLayout LLayout = (LinearLayout) tw.getChildAt(i);
  TextView currenttab = (TextView) LLayout.getChildAt(0);

  if (tab.equals(currenttab.getText())) {
    th.setCurrentTab(i);
    TextView tabContent = (TextView) th.getTabContentView().getChildAt(0);
    tabContent.append(linea);
    return true;
  }
}

问题是getTabContentView()仅引用当时活动的Tab,所以如果我想在非选择的选项卡的TextView中添加一些文本,我必须先使用.setCurrentTab(i)选择它,这意味着我要改变活动标签......

我认为这比我想的要容易得多......对此有何帮助?

谢谢!

---------编辑---------

那时我意识到无法访问非活动选项卡的内容。至少不那样。据说您无法访问非活动选项卡的视图,因为它没有关联的视图,至少不是公共视图。

我试图声明一个Map,其中第一个String是选项卡的名称,第二个是与其内容关联的TextView,并在创建选项卡时分配它们。结果是我只能引用活动选项卡的TextView,其他的引发了NullPointer异常。

所以我以不同的方式关注问题。现在,如果我必须将一些文本添加到非活动选项卡中,我将该文本添加到哈希,我声明了一个OnTabChangeListener,每次我更改选项卡时,我都会将待处理内容转储到其中。

这不是最出色的解决方案,但它是我工作的唯一一个......所以,如果有人有更好的解决方案,我将不胜感激。

android android-layout android-activity android-tabhost android-view
4个回答
0
投票

可以通过TabWidget访问选项卡:

yourTabHost.getTabWidget().getChildTabViewAt(position);

此代码返回任何选项卡的视图,甚至是非活动选项卡。


0
投票

首先使用tabHost.getChildAt(n)获取整个选项卡的视图,然后在选项卡视图中执行findViewById以应用更改。

例:

View tabView = tabHost.getChildAt(0);
View myView = tabView.findViewById(R.id.myView);
myView.setVisibility(View.INVISIBLE);

0
投票

@nKn,感谢getTabContentView()。在我的情况下,它不返回选项卡,而是包含所有选项卡的FrameLayout,以下工作:

View tab = tabHost.getTabContentView().getChildAt(2);
ImageView image = (ImageView) tab.findViewById(R.id.image);

0
投票

TabHost子选项卡视图可以通过TabWidget方法getChildTabViewAt(position)访问

    TabWidget tabWidget = tabHost.getTabWidget();

    for (int i = 0; i < tabWidget.getTabCount(); i++) {

        View parentView = tabWidget.getChildTabViewAt(i);

    }

选择的标签视图可以通过以下方式访问:

View parentView = tabHost.getCurrentTabView();
© www.soinside.com 2019 - 2024. All rights reserved.