android - 可扩展 ListView 项目高度,其中包含另一个 ListView

标签 android listview height expandable

我有一个 ExpandableListView,其中每个组都有一个子项。每个子项都是另一个具有不同行数的 ListView。 ExpandableListView 项目高度在展开状态下存在问题,它不会换行到子 ListView 内容。我只能在子 ListView 布局中设置具体的高度值。但我想要一个 ExpandableListView 自动包装每个 child 。可能吗?

getChildView() 来 self 的 ExpandableListAdapter:

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if( convertView == null ) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        convertView = inflater.inflate( R.layout.anit_object_data, parent, false );
    }
    ListView lv = (ListView)convertView.findViewById( R.id.listData );
    List<ListData> valuesList = getItems().get(groupPosition).getListData( getContext() );
    ListDataAdapter adapter = new ListDataAdapter( getContext(), android.R.layout.simple_list_item_1, valuesList );
    lv.setAdapter(adapter);

    return convertView;
}


子 ListView 的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id = "@+id/listHolder"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/listData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:scrollbars="none" >
        </ListView>

    </LinearLayout>

</LinearLayout>



我试图在适配器的 getChildView() 中测量子 ListView 的高度,但 ExpandableListView 展开的项目高度仍然不正确。

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if( convertView == null ) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        convertView = inflater.inflate( R.layout.anit_object_data, parent, false );
    }
    ListView lv = (ListView)convertView.findViewById( R.id.listData );
    List<ListData> valuesList = getItems().get(groupPosition).getListData( getContext() );
    ListDataAdapter adapter = new ListDataAdapter( getContext(), android.R.layout.simple_list_item_1, valuesList );
    lv.setAdapter(adapter);

    lv.measure( MeasureSpec.EXACTLY, MeasureSpec.UNSPECIFIED );
    convertView.getLayoutParams().height = lv.getMeasuredHeight();

    return convertView;
}

最佳答案

好吧,我已经试过你的想法了,组 subview 是一个 ListView 。但结果并不好。因为 subview ( ListView )的高度很难测量。所以我按照正常方式更改了解决方案: subview 只是一个项目,就像 ListView 的项目一样。这样我们也重写了 getChildView() 方法。

关于android - 可扩展 ListView 项目高度,其中包含另一个 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15198652/

相关文章:

android - 应该使用android :showAsAction when not using the appcompat library

android - 如何获取数据来自 Json 格式的服务器的 ListView 所选项目的 ID

JavaScript - 获取浏览器高度

android - autocompletetextview setonitemselectedlistener 不工作

android - 无法将计数值设置为列

android - 发布到 Google Play 商店后,MissingPluginException(在 channel plugins.flutter.io/google_sign_in 上找不到方法 init 的实现)

c# - Windows Phone 8.1 listview 点击更改页面

flutter - 如何使用 ListTile 记住和突出显示选定的抽屉项目?

javascript - 具有简单可变高度的 Div

html - h1 行高或悬停颜色变化时的高度问题