右侧的Listview和右侧的tabhost,屏幕共享率为20:80

JNI*_*oad 5 android android-widget android-layout android-listview android-tabhost

嗨,我添加了两个TabHosts Activity并且工作正常.我添加了另一个XML文件,ListViewActivity在我使用的地方调用Tabhost.它正如预期的那样给出错误,但我尝试了很多但无法解决它.谁能给我关于如何管理任何想法ListView,并TabHost在一两个Activity?我没有放入代码,因为它主要来自教程.任何帮助,将不胜感激.谢谢.主要错误是"您的内容必须具有TabHost其id属性为'android.R.id.tabhost'"

sol*_*ito 0

带有选项卡和带有选中项的 ListView 的示例活动:

我在下面发布了一个示例活动,其中包含TabHost3Tabs和 2 个ListView,每个 1 个tab1tab3。我使用 来ArrayList初始化我的,ArrayAdapter我可以从中链接我的ListView. 然后,我通过其,tab3ListView设置后将项目添加到ArrayListtab3List请注意,tab3ListView 的项目有一个相应的复选框。请注意,现在有些人ActionBar.Tabs更喜欢这种方式TabHost,但我发现这种方式更简单。

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TabHost
    android:id="@+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" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="70dp"
            android:layout_marginBottom="-4dp"
            android:layout_weight="0" >
        </TabWidget>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="0dp"
            android:layout_weight="1">
            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <ListView 
                android:id="@+id/tab1List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fastScrollEnabled="true"></ListView>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >
           <EditText 
                android:id="@+id/tab2Edit1"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:hint="Enter text here"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >
           <ListView 
                android:id="@+id/tab3List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fastScrollEnabled="true"></ListView>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

主要活动:

public class MainActivity extends Activity{

// TabHost
TabHost th;
LinearLayout tab1, tab2, tab3;

// ListView
ListView tab1ListView, tab3ListView;

// String list
String stringList[] = {"Paris", "Washington", "Amsterdam", "London", "Bale", "Madrid",
        "Zurich", "Munich", "Berlin", "Milan", "Rome", "Turin", "Lyon", "Marseille"};

// Adapters
ArrayList<String> tab1List, tab3List;
ArrayAdapter<String> tab1Adapter, tab3Adapter;

// Others
EditText tab2Edit1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Initialize Layout
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Initialize Tabs
    th = (TabHost) findViewById(R.id.tabhost);
    th.setup();
    // Tab1
    TabSpec spec1 = th.newTabSpec("tag1");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator("Tab 1");
    th.addTab(spec1);
    // Tab2
    TabSpec spec2 = th.newTabSpec("tag2");
    spec2.setContent(R.id.tab2);
    spec2.setIndicator("Tab 2");
    th.addTab(spec2);
    // Tab3
    TabSpec spec3 = th.newTabSpec("tag3");
    spec3.setContent(R.id.tab3);
    spec3.setIndicator("Tab 3");
    th.addTab(spec3);

    //Initialize Adapter and ListView from Tab1
    tab1ListView = (ListView) findViewById(R.id.tab1List);
    tab2Edit1 = (EditText) findViewById(R.id.tab2Edit1);
    tab1List    = new ArrayList<String>(Arrays.asList(stringList));
    tab1Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tab1List);
    tab1ListView.setAdapter(tab1Adapter);

    //Initialize Adapter and ListView from Tab3
    tab3ListView = (ListView) findViewById(R.id.tab3List);
    tab3List    = new ArrayList<String>(Arrays.asList(stringList));
    tab3Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, tab3List);
    tab3ListView.setAdapter(tab3Adapter);
    tab3ListView.setFocusable(false);
    tab3ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    // Modify ListView: modify ArrayList and call notifyDataSetChanged() from corresponding adapter
    tab3List.add(0,"New item at location 0");
    tab3List.add(3,"New item at location 3");
    tab3List.add("New item at location end");
    tab3Adapter.notifyDataSetChanged();

    tab3ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // Set what happenss when you check or uncheck an item here 
        }

    });
} 
Run Code Online (Sandbox Code Playgroud)