ListView在同一活动上有三个选项

时间:2014-12-19 05:01:05

标签: android listview android-tabhost segmentedcontrol

我有一个列表视图,现在我正在尝试为此列表视图设置标签视图。

现在有时会有3个选项,有时是2个,有时是1.正如你在这里看到的那样

enter image description here

单击选项卡时,我将使用新数据重新加载列表视图,具体取决于单击选项卡栏中的项目。但这是类似的数据,所以它将在相同的列表视图中,我想使用相同的xml布局。但目前我无法做到这一点,我看不到让它发挥作用。

这就是我所拥有的

 myTabHost =(TabHost) findViewById(R.id.TabHost01);
    myTabHost.setup();

    TabHost.TabSpec spec1 = myTabHost.newTabSpec("First Tab");
    spec1.setIndicator("First Tab", getResources().getDrawable(android.R.drawable.ic_menu_add));
    spec1.setContent(R.id.tab1);
    myTabHost.addTab(spec1);

    myTabHost.addTab(myTabHost.newTabSpec("Second Tab").
            setIndicator("Second Tab", getResources().getDrawable(android.R.drawable.ic_menu_edit)).setContent(R.id.tab2));

这是为2个标签设置,然后在xml中设置

<include
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                layout="@layout/item_list_view">


            </include>

            <include
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                layout="@layout/test">


            </include>

但是我想使用相同的布局而不是不同的布局,只是重新加载数据但是当我将标签.setContent设置为相同的ID时它不起作用?

所以基本的问题是如何在多个选项卡上使用相同的xml,只在列表视图中加载不同的数据?

如果重要,标签栏将填充文字而不是图像。看了一些关于这个的但是对我的情况没有帮助 https://www.youtube.com/watch?v=OeNC_sShJXs https://www.youtube.com/watch?v=1-u3toC6ctY

所以我需要一些帮助来设置它。

感谢您的帮助:)

2 个答案:

答案 0 :(得分:2)

将TabHost视图添加到布局中。您可以使用addTab()方法动态添加任意数量的选项卡。

public class MainActivity extends Activity {

private TabHost myTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTabHost =(TabHost) findViewById(R.id.TabHost01);
            mytabhost.setup();

TabSpec spec = mytabhost.newTabSpec("tab_creation");
spec.setIndicator("Create adresse",getResources().getDrawable(android.R.drawable.ic_menu_add));
    spec.setContent(R.id.onglet1);
    mytabhost.addTab(spec);

    mytabhost.addTab(mytabhost.newTabSpec("tab_inser").setIndicator("Delete",getResources().getDrawable(android.R.drawable.ic_menu_edit)).setContent(R.id.Onglet2));

   mytabhost.addTab(mytabhost.newTabSpec("tab_affiche").setIndicator("Show All",getResources().getDrawable(android.R.drawable.ic_menu_view)).setContent(R.id.Onglet3));

   ListView lv=(ListView) findViewById(R.id.listView1);
   MyListAdapter adapter =new MyListAdapter(this);
   lv.setAdapter(adapter);              
 }
}

完整参考:http://mrbool.com/how-to-create-an-activity-android-with-a-tabhost-view/27990

答案 1 :(得分:2)

据我了解,这个问题涉及两个部分:

  1. 以编程方式创建标签。
  2. 在标签之间共享相同的内容视图。
  3. 对于第一部分,您可以在布局中放置一个“空”TabHost,然后根据要显示的标签调用addTab()。例如,布局文件只是:

    <?xml version="1.0" encoding="utf-8"?>
    <TabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/TabHost01"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
            </FrameLayout>
        </LinearLayout>
    </TabHost>
    

    然后,在onCreate()

    TabHost tabHost = (TabHost)findViewById(R.id.TabHost01);
    tabHost.setup();
    tabHost.addTab(tabHost.newTabSpec(...));
    tabHost.addTab(tabHost.newTabSpec(...));
    

    每个TabHost.TabSpec指定每个标签的指标和内容。

    现在,至于第二部分,您需要一个单个 ListView小部件,并在用户切换标签时更改其数据。这实际上非常简单,因为TabHost可以处理多个选项卡具有相同内容而没有问题的情况。您只需为每个标签设置TabHost.TabContentFactory,并让每个标签返回相同的视图。

    例如:

    mListView = new ListView(this);
    
    TabHost tabHost = (TabHost)findViewById(R.id.TabHost01);
    tabHost.setup();
    tabHost.addTab(tabHost.newTabSpec("1").setIndicator("First").setContent(mDummyTabContent));
    tabHost.addTab(tabHost.newTabSpec("2").setIndicator("Second").setContent(mDummyTabContent));
    tabHost.addTab(tabHost.newTabSpec("3").setIndicator("Third").setContent(mDummyTabContent));
    

    其中mTabContent初始化为:

    private final TabHost.TabContentFactory mDummyTabContent = new TabHost.TabContentFactory()
    {
        @Override
        public View createTabContent(String tag)
        {
            return mListView;
        }
    };
    

    然后,最后一步是添加一个TabHost.OnTabChangeListener来切换单ListView中的数据:

    tabHost.setOnTabChangedListener(mOnTabChangedListener);
    mOnTabChangedListener.onTabChanged("1");
    

    其中:

    private TabHost.OnTabChangeListener mOnTabChangedListener = new TabHost.OnTabChangeListener()
    {
        @Override
        public void onTabChanged(String tabId)
        {
            String[] data;
    
            if (tabId.equalsIgnoreCase("1"))
                data = FIRST_ITEMS;
            else if (tabId.equalsIgnoreCase("2"))
                data = SECOND_ITEMS;
            else
                data = THIRD_ITEMS;
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(FakeTabViewActivity.this, android.R.layout.simple_list_item_1, data);
            mListView.setAdapter(adapter);
        }
    };
    

    包含活动和布局文件的示例要点here