Android:嵌套 ListView 和项目高度

标签 android listview nested

我目前正在尝试创建一个嵌套的 ListView。

外部 ListView 包含项目,其中每个项目都包含一个 TextView 作为标题,以及另一个带有子项的 ListView。

使用另一个嵌套的 ListView 适配器填充 ListView 效果非常好,但是,当涉及到在设备上实际显示内容时,嵌套列表项的高度不足以显示所有子项。准确地说,只显示第一个嵌套列表项。

我目前的布局方法如下:

外部列表项:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:padding="5px"
        >

    <TextView android:id="@+id/day"
              android:textSize="14sp"
              android:textStyle="bold"
              android:textColor="#ff5e00"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:scrollHorizontally="false"/>


    <ListView android:id="@+id/innerList"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:fadingEdge="none"
            />
</LinearLayout>

和内部列表布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:padding="5px"
        >

    <TextView  android:id="@+id/innertextview"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:scrollHorizontally="false"
               android:singleLine="false"/>
</LinearLayout>

是否有机会以显示所有内部列表项的方式调整外部列表项的大小?

我还查看了 ExpandableListViews。这种方法有效,但我不想折叠列表项。

非常感谢, 马蒂亚斯

最佳答案

我不太喜欢这个解决方案,所以我进一步研究了这个问题并找到了另一个解决方案:

ExpandableListView 继承自ListView。它所做的是覆盖 BaseAdapter 的 getView 方法并区分子项和父项。它不是真正的嵌套列表,而是通过使用不同的布局项将列表扁平化。

同样可以创建 NestedListView。继承自 ListView 并期望有一个自创建的适配器,它可以区分子布局项和父布局项。 对于每个返回适当填充的布局项。

完成。

关于Android:嵌套 ListView 和项目高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8155529/

相关文章:

android - 处理传入消息我的 broadCastReceiver 不工作

android - 如何从自定义 ListView 中获取选定的项目?

Android ListView setOnScrollListener

wpf - 在包含按钮的 ListView 中,如何获取单击的索引?

c++函数调用函数不起作用

android - 'Paint'参数在android.graphics.Canvas.drawBitmap()中有什么作用?

android - 按钮卡住了

android - 厂商时钟/闹钟包及类名列表,请加

css - 嵌套 Bootstrap 行

php - 是否可以在 PHP 的 Echo 语句中包含 IF 语句