ListView控件与同一活动的三个选项 [英] ListView with three options on same activity

查看:199
本文介绍了ListView控件与同一活动的三个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,现在我想建立这个列表视图选项卡视图。

I have a list View and now I am trying to set up tab view for this list view.

现在的某个时候有3个选项,有时2,有时1.正如你可以在这里看到。

Now sometime there are 3 options, sometimes 2 and sometimes 1. As you can see here

和点击一个选项卡时,我会重新与我根据这在标签栏项目被点击了新的数据列表视图。但是,这是类似的数据,所以它会在同一个列表视图,我想使用相同的XML布局。但目前我无法做到这一点,我看不到得到它的工作了。

And when a tab is clicked I will reload my list view with new data depending on which item in the tab bar was clicked. But this is similar data, so it will be in the same list view and I want to use the same xml layout. But currently I am unable to do this, I can't see to get it working it.

下面是我有这么

 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我有

This is setting it up for 2 tab, and then in the xml I have

<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这是行不通的?

But I want to use the same layout instead of different ones and just reload the data but when I set the tabs .setContent to the same id it doesn't work?

所以,基本的问题是我怎么使用相同的XML为多个选项卡,只加载不同的数据在列表视图?

So the basic question is how do I use the same xml for more than one tab, and just load different data in the list view?

如果该事项的标签栏将充满不文本图像。已经看过一些这方面的tutorals,但不是我的情况有所帮助
https://www.youtube.com/watch?v=OeNC_sShJXs
https://www.youtube.com/watch?v=1-u3toC6ctY

The tab bar will be filled with text not images if that matters. Have looked at some tutorals on this but aren't helpful for my situation https://www.youtube.com/watch?v=OeNC_sShJXs https://www.youtube.com/watch?v=1-u3toC6ctY

所以,我需要一些帮助设置此功能。

So I need some help setting this up.

感谢您的帮助提前:)

推荐答案

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

As I understand it, this question involves two parts:


  1. 创建标签编程。

  2. 共享选项卡之间相同的内容视图。

在第一部分,你可以把一个空 TabHost 在布局,然后调用<一个href=\"https://developer.android.com/reference/android/widget/TabHost.html#addTab%28android.widget.TabHost.TabSpec%29\"相对=nofollow> addTab() 后,这取决于你想显示的选项卡。例如,布局文件将只是:

For the first part, you can put an "empty" TabHost in your layout, and then call addTab() later, depending on the tabs you want to show. For example, the layout file would be just:

<?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 指定每个选项卡的指标和内容。

Where each TabHost.TabSpec specifies the indicator and content of each tab.

现在,作为第二部分,你需要一个 的ListView 部件和用户切换标签更改其数据。这实际上是pretty容易,因为 TabHost 可处理有多个选项卡有没有问题,同样的内容的情况下。你只需要设置 TabHost.TabContentFactory 每个标签,并让每个人返回的相同的看法。

Now, as to the second part, you need a single ListView widget and change its data as the user switches tabs. This is actually pretty easy, since the TabHost can handle the case where multiple tabs have the same content without problems. You just need to set up a TabHost.TabContentFactory for each tab, and have each of them return the same view.

例如:

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));

其中, mTab​​Content 被初始化为:

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);
    }
};

与活性,布局文件示例要点可以这里

这篇关于ListView控件与同一活动的三个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆