Listview一次只显示一行

3

我遇到了一个问题,列表视图一次只能显示一行,即使有很多行。这个列表视图包含在一个选项卡中,所以问题可能不在列表视图中,而是在选项卡中。 我首先尝试在单独的布局文件中定义列表视图,并在创建选项卡时使用该文件。当那样做不起作用时,我将其放入带有选项卡定义的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));

依然如故,一次只能处理一行。


我要冒昧猜测一下,可能是因为您将ListView放在了FrameLayout中。尝试改用垂直方向的LinearLayout来替代"@android:id/tabcontent"。 - Rob S.
你能滚动查看其他项目吗?还是只显示一个项目? - Vinay W
我在很多例子中都看到过这样的写法。 - theblitz
我可以滚动到它们所有的位置。只是似乎创建了一个高度为一个条目的列表视图。 - theblitz
3个回答

0

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

代码:

 layout_height ="150dp"

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

你是动态添加选项卡吗?所以你是用这种方式添加的吗?


我这样添加选项卡是因为我将适配器附加到它们上面。最简单的方法是通过TabFactory来实现。我尝试按照您给出的方式更改布局,但没有成功。 - theblitz

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"


这只影响选项卡标题的文本,而不影响选项卡内部的列表。 - theblitz
抱歉,但列表在选项卡内吗?例如tab1?您的布局将列表设置在选项卡下方。 - Ant4res
奇怪。它在列表中。当我复制代码时发生了一些事情。已经纠正。 - theblitz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接