4

我有一个带有两个选项卡的 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);

任何提示?

4

3 回答 3

9

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

tabHost.setCurrentTab(0);

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

tabHost.setCurrentTab(1);

然后是,列表显示,一切都很好。至于为什么会发生这种情况,对我来说仍然是一个谜。

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

于 2011-05-26T17:22:19.460 回答
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);
于 2012-03-16T00:41:01.517 回答
-2

您只需执行另一项存在列表视图的活动并放入以下活动

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希望这可能会有所帮助..

希望这可以解决问题..

于 2011-04-10T13:25:18.280 回答