1

在我的应用程序中,我有 4 个选项卡,其中一个选项卡中有一个列表(tab3)。When 1 of the list options are selected it goes to another activity but then the tabs I have go away. 即使我切换到另一个活动,有没有办法让它们永久存在?

我有将选项卡放在每个 XML 和 Java 类中的想法,但这似乎是多余的,我认为它会显示选项卡在我不想要的不同活动中移动,我希望它们在整个应用程序中保持静止。

代码:

public class MainActivity extends Activity {

TabHost th;
TabSpec specs;
ListView libraryView;
String libraryList[] = {"Item1", "Item2"};

@Override
public void onCreate(Bundle savedInstanceState) {
    //Always do this at the start.
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_white_text, R.id.library_list_content, libraryList);
    libraryView = (ListView) findViewById(R.id.listView1);
    libraryView.setAdapter(adapter);

    libraryView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            String librarySelected = libraryList[position];
            Class libraryClass;
            try {           
                libraryClass = Class.forName("com.jj.test.library." + librarySelected);
                Intent libraryIntent = new Intent(MainActivity.this, libraryClass);
                startActivity(libraryIntent);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });


    //Creating the TabHost and gathering its ID for usage.
    th = (TabHost) findViewById(R.id.tabhost);        
    th.setup();

    specs = th.newTabSpec("tag1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("tab1");
    th.addTab(specs);

    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.tab2);
    specs.setIndicator("tab2");
    th.addTab(specs);

    specs = th.newTabSpec("tag3");
    specs.setContent(R.id.tab3);
    specs.setIndicator("tab3");
    th.addTab(specs); 

    specs = th.newTabSpec("tag4");
    specs.setContent(R.id.tab4);
    specs.setIndicator("tab4");
    th.addTab(specs);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/black" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

                <ListView
                    android:id="@+id/listView1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >
                </ListView>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab4"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            </LinearLayout>

        </FrameLayout>

        <TabWidget android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_marginBottom="-4dp" >
        </TabWidget>
    </LinearLayout>
</TabHost>

</RelativeLayout>

当他们单击 Item1 或 Item2 (位于 tab3 的列表中并将它们带到不同的 Activity 时)它不会带上标签。我需要对 MainActivity XML 或 Class 做些什么来使其在整个应用程序中持久化吗?

4

0 回答 0