【问题标题】:Applying tabs with views and not separate activities应用带有视图的选项卡而不是单独的活动
【发布时间】:2012-03-30 19:01:37
【问题描述】:

我无法弄清楚如何将我当前的选项卡设置转换为使用视图而不是单独活动的选项卡设置...我在调用搜索功能时遇到问题,我认为这是由于我创建的方式我的标签。

我的主要启动器活动是 public class Menu extends TabActivity,它创建了标签

 intent = new Intent().setClass(this, TabGroup1.class);
 // Initialize a TabSpec for each tab and add it to the TabHost
 spec = tabHost.newTabSpec("codes").setIndicator("All Codes",res.getDrawable(R.drawable.ic_tab_codes))
 .setContent(intent);
 tabHost.addTab(spec);

`TabGroup1' 对每个选项卡执行以下操作

public class TabGroup1 extends TabGroupActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startChildActivity("Category", new Intent(this,Category.class));
}
}

然后调用ListActivity,它会在单击项目时从那里显示内容,然后创建另一个意图,然后启动一个新活动,允许我在用户进入列表时在每个级别上都有标签。

这是通过以下代码完成的

    public void onListItemClick(ListView parent, View view, int position, long id) {
    Intent intent = new Intent(getParent(), SubCategory.class);
    Cursor cursor = (Cursor) adapter.getItem(position);
    intent.putExtra("CATEGORY", cursor.getString(cursor.getColumnIndex("_id")));
    /*startActivity(intent);*/
    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
    parentActivity.startChildActivity("SubCategory", intent);
}

TabGroupActivity 是我从tutorial 中找到的一个类,它允许您在同一个选项卡布局下拥有多个活动。

我正在努力将我必须使用的视图转换为使用 setContent 来更改视图。

我找到了this 示例,但它没有提供足够的详细信息让我继续。 还找到了this 一个……

谁能提供我需要更改的内容以及如何使用我的 listactivities 设置内容...

提前致谢

【问题讨论】:

  • 我很困惑...您当前每次更改标签时都在创建新标签????
  • 嗨@Barak 我已经编辑了我上面的问题,并添加了一些关于每个活动如何调用下一个活动的代码,以便选项卡位于顶部......它可以工作,但我想从这个转换因为我读过它不是最好的方法,而且 ActivityGroup 已被弃用。这个tutorial 是我用来创建标签的那个。

标签: java android android-listview


【解决方案1】:

有几件事... setContent 定义了内容,它不会导致选项卡的切换。如果您想强制更改某个选项卡,请使用TabHost.setCurrentTab(tabid);,否则默认为第一个选项卡,然后是用户选择的任何选项。

我自己的一个项目中的一个示例(这个项目实际上有四个选项卡,但我已经剪掉了一些以尝试保持这一点)。有几种方法可以做到这一点,但我发现最简单的方法是创建一个填充每个选项卡的方法,这样我可以通过为我需要的任何选项卡调用适当的方法来根据需要刷新选项卡(包含下面的所有 java在 TabActivity 中)。

setupdetailmain.xml

<?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:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="5dp" >
            <TextView
                    android:id="@+id/setupheader"
                    android:layout_width="fill_parent"
                    android:layout_height="20dp"
                    android:textSize="15dp" />
            <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:gravity="bottom" />
            <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="5dp" >
            <!-- General Info Tab -->
                    <LinearLayout
                            android:id="@+id/note_tab"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >
                    </LinearLayout>
            <!-- Tool Tab -->
                    <LinearLayout
                            android:id="@+id/offset_tab"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >
                            <ListView
                                    android:id="@+id/list2"
                                    android:layout_width="match_parent"
                                    android:layout_height="0dp"
                                    android:layout_weight="1"
                                    android:drawSelectorOnTop="false" />
                    </LinearLayout>
            </FrameLayout>
    </LinearLayout>
</TabHost>

标签设置代码(扩展 TabActivity)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setupdetailmain);

    // ***************************************************************
    // Set up the tabs in the tabhost
    // ***************************************************************
    tabHost = getTabHost();
    TabHost.TabSpec spec;
    spec = tabHost.newTabSpec("Offsets").setIndicator("Offsets")
            .setContent(R.id.offset_tab);
    tabHost.addTab(spec);
    spec = tabHost.newTabSpec("Notes").setIndicator("Notes")
            .setContent(R.id.note_tab);
    tabHost.addTab(spec);
    populateTabs(StartTab);
}

选项卡填充方法

// ***************************************************************
// Fill the Notes tab
// ***************************************************************
private void notestab() {
    notes = (LinearLayout) findViewById(R.id.note_tab);
    notestxt = new TextView(this);
    notestxt.setText(SETUPINFO_NOTES);
    notestxt.setTextSize(15);
    notes.addView(notestxt);
}

// ***************************************************************
// Fill the Offset tab
// ***************************************************************
private void offsettab() {
    wpccount = 0;
    for (int i = 0; i < 20; i++) {
        if (wpcdesc[i] != null) {
            wpcdisplayhold[wpccount] = wpcid[i] + " - " + wpcdesc[i];
            wpcidhold[wpccount] = wpcid[i];
            wpcdeschold[wpccount] = wpcdesc[i];
            wpccount++;
        }
    }
    wpcdisplay = new String[wpccount];
    for (int i = 0; i < wpccount; i++) {
        wpcdisplay[i] = wpcdisplayhold[i];
    }
    mWPCView = (ListView) findViewById(R.id.list2);
    mWPCView.setAdapter(new ColorizingListAdapter(SetupDisplay.this,
            wpcdisplay, "Offset"));
    registerForContextMenu(mWPCView);
}

这使用了一个自定义适配器,但希望它能够理解如何在不执行新活动的情况下在选项卡视图中设置列表。

【讨论】:

    猜你喜欢
    • 2011-02-27
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多