TabHost在开头没有显示内容

时间:2011-04-10 12:07:35

标签: android tabactivity

我有一个带有两个标签的TabActivity。活动的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost 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:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:padding="5dp">

    <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:padding="5dp">
        <ListView android:id="@+id/list"
            android:layout_width="fill_parent" android:layout_height="fill_parent" />

    </FrameLayout>
</LinearLayout>
</TabHost>

布局包含一个ListView,它在setOnTabChangedListener()中相应地填充。填写列表并显示它没有问题。

我的问题是活动开始时列表视图没有显示,虽然我通过ID找到它并填充它;它仅在我更改选项卡后填充。

并且onCreate(..)中的代码如下所示:

l = (ListView) findViewById(R.id.list);
l.setAdapter(new CommentsAdapter(this, (JSONArray) obj));

TabSpec spec;
    spec = tabHost.newTabSpec("0").setIndicator("0",
            res.getDrawable(R.drawable.tab_recent)).setContent(
            R.id.list);
    tabHost.addTab(spec);

    spec = tabHost.newTabSpec("1").setIndicator("1",
            res.getDrawable(R.drawable.tab_pop)).setContent(
            R.id.list);
    tabHost.addTab(spec);

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
            int tab = Integer.valueOf(tabId);
            showDialog(WAIT_DIALOG);

            switch (tab) {
            // recent
            case 0:
                //refill the list
                break;
            // popular

            case 1:
                //refill the list
                break;
            }
        }
    });

    tabHost.setCurrentTab(0);

任何提示?

3 个答案:

答案 0 :(得分:9)

我有类似的问题。我注意到的是,如果你这样做

tabHost.setCurrentTab(0);

然后onTabChanged未被触发。但是,如果你这样做

tabHost.setCurrentTab(1);

然后就是,列表显示出来并且一切都很好。至于为什么会发生这种情况,对我来说仍然有些神秘感。

使用此作弊在第一个标签上启动您的应用: 在onCreate()中,在您首先创建选项卡调用之后: tabHost.setCurrentTab(1); 然后打电话: tabHost.setCurrentTab(0);

答案 1 :(得分:1)

您可以尝试使用不同的布局,比如说

spec = tabHost.newTabSpec("0").setIndicator("0",
        res.getDrawable(R.drawable.tab_recent)).setContent(
        R.id.list1);
tabHost.addTab(spec);

spec = tabHost.newTabSpec("1").setIndicator("1",
        res.getDrawable(R.drawable.tab_pop)).setContent(
        R.id.list2);
tabHost.addTab(spec);

答案 2 :(得分:-2)

你只是做另一个列表视图存在的活动&amp;把下面的一个

Intent i1 = new Intent(tabs.this,listDisplay.class);
spec=tabHost.newTabSpec("0").setIndicator("0",res.getDrawable(R.drawable.                        tab_recent)).setContent(i1);                                         

同时放tabHost.setCurrentTab(0);ontabChangeListener之前希望这可能会有所帮助..

希望这可以解决问题..