Listview一次只显示一行

时间:2012-07-15 13:09:31

标签: android android-listview android-tabhost

我遇到列表视图的问题,即使行数很多,也只会一次显示一行。 列表视图包含在选项卡中,因此可能存在问题而不是列表视图中的问题。 我首先尝试在单独的布局文件中定义列表视图,并在创建选项卡时使用它。当这不起作用时,我将它放入带有选项卡定义的FrameLayout中。 结果相同。

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost">
    <LinearLayout android:id="@+id/LinearLayout01"
        android:orientation="vertical" android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_height="fill_parent" android:layout_width="wrap_content"></TabWidget>
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_height="fill_parent" android:layout_width="fill_parent">
            <ListView android:id="@+id/list1" android:layout_width="fill_parent"
                android:layout_height="fill_parent"> 
            </ListView></FrameLayout>
    </LinearLayout>
</TabHost>

这是我用来创建和添加标签的代码:

protected void addTab(String tabName, TabHost.TabContentFactory tabFactory){
    TabSpec tabSpec = tabHost.newTabSpec(tabName);
    tabSpec.setIndicator(tabName);
    tabSpec.setContent(tabFactory);
    tabHost.addTab(tabSpec);
    tabs.add(new TabDetails(tabName,tabFactory));

}

我称之为:

addTab("Medical Center Tests",this);

并包含此代码以创建标签内容:

@Override
public View createTabContent(String tag) {


    // Get the data returned from the servelet and display it in the ListView
    data = getDataArray(this.getIntent().getExtras());




    ListView lv = (ListView) contentView.findViewById(R.id.list1);

lv.setAdapter(new MyMedicalCentersTestsScreenAdapter(this,lv,data));
return lv;
}

数据就在那里。它只是列表视图的高度等于一行的高度。

修改

我添加了一些调试命令来查看getView请求的位置。它只请求第一行,然后在滚动时一次请求一行。 因此,它肯定会将其视为一个小型列表视图并相应地滚动它们。

编辑2

决定尝试一下实验。 返回列表视图的简单列表。

    ListView lv = (ListView) contentView.findViewById(R.id.list1);
    // create some dummy strings to add to the list
    List<String> list1Strings = new ArrayList<String>();
    list1Strings.add("Item 1");
    list1Strings.add("Item 2");
    list1Strings.add("Item 3");
    list1Strings.add("Item 4");
    lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));

还是一样。一次只有一行。

3 个答案:

答案 0 :(得分:0)

尝试这样做

<TabWidget android:id="@android:id/tabs" android:layout_height="fill_parent" android:layout_width="fill_parent"></TabWidget>

而不是

<TabWidget android:id="@android:id/tabs" android:layout_height="fill_parent" android:layout_width="wrap_content"></TabWidget>

android:layout_height="wrap_content"android:layout_height="100dp"

而不是

android:layout_height="fill_parent"

答案 1 :(得分:0)

你定义布局的高度而不是使用fill_parent ......

代码:

 layout_height ="150dp"

答案 2 :(得分:0)

您的布局有点奇怪,因为tabWidget(它显示了表示父级标签集中每个页面的标签标签列表)高度设置为fill_parent。这样的事怎么样?

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
         android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@android:id/tabhost">
<LinearLayout
         android:id="@+id/LinearLayout01"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical" >
<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="match_parent"
    android:layout_height="84dp" >

</TabWidget>

<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

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


        <ListView
            android:id="@+id/list1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

        </ListView>

    </LinearLayout>

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

    </LinearLayout>

  </FrameLayout>

</LinearLayout>
</TabHost>

对于标签:

    tabhost = (TabHost) findViewById(android.R.id.tabhost);
    tabhost.setup();

    TabSpec ts = tabhost.newTabSpec("tag1"); 
    ts.setContent(R.id.tab1);
    ts.setIndicator("TAB1");
    tabhost.addTab(ts);

    ts = tabhost.newTabSpec("tag2"); 
    ts.setContent(R.id.tab2);
    ts.setIndicator("TAB2");  
    tabhost.addTab(ts);

您是否以这种方式添加标签'因为您动态添加标签?

相关问题