Java Android |如何在滚动视图中使用ExpandableListView?

Java Android |如何在滚动视图中使用ExpandableListView?,java,android,scrollview,expandablelistview,Java,Android,Scrollview,Expandablelistview,我的xml文件中有两个expandablelistview,还有一些textview。当我展开第一个(在顶部)ExpandableListView时,它会将下面的所有内容压缩在一起,而第二个(在底部)ExpandableListView只剩下很少的空间。用户只能通过在非常小的范围内滚动来查看第二个ExpandableListView的内容 我已经想过将它包装在一个滚动视图中,但是我的可扩展列表视图只显示第一个组,不可扩展。此外,如果我将高度设置为静态数字,并且所有组都已展开,则无法再看到内容 我

我的xml文件中有两个
expandablelistview
,还有一些
textview
。当我展开第一个(在顶部)
ExpandableListView
时,它会将下面的所有内容压缩在一起,而第二个(在底部)
ExpandableListView
只剩下很少的空间。用户只能通过在非常小的范围内滚动来查看第二个
ExpandableListView
的内容

我已经想过将它包装在一个
滚动视图中
,但是我的
可扩展列表视图
只显示第一个组,不可扩展。此外,如果我将高度设置为静态数字,并且所有组都已展开,则无法再看到内容

我希望
expandablelistview
都可以展开,并且我可以上下滚动布局,以便所有内容都有足够的空间。 有什么建议吗

我的XML.File

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="?attr/colorPrimary"
            android:paddingLeft="16dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:text="Group1"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Attention1"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp" />

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/elv_theorie"
            android:layout_gravity="center_horizontal"
            android:groupIndicator="@android:color/transparent"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="?attr/colorPrimary"
            android:paddingLeft="16dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:text="Group2"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Attention2"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"/>

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/elv_pruefung"
            android:layout_gravity="center_horizontal"
            android:groupIndicator="@android:color/transparent" />

</LinearLayout>

我的问题:

使用
滚动视图
仅显示第一组,无法展开


您应该使用此功能设置可扩展listview的动态高度

private void setListViewHeight(ExpandableListView listView,
                               int group) {
    ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
            View.MeasureSpec.EXACTLY);
    for (int i = 0; i < listAdapter.getGroupCount(); i++) {
        View groupItem = listAdapter.getGroupView(i, false, null, listView);
        groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += groupItem.getMeasuredHeight();
        if (((listView.isGroupExpanded(i)) && (i != group))
                || ((!listView.isGroupExpanded(i)) && (i == group))) {
            for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                View listItem = listAdapter.getChildView(i, j, false, null,
                        listView);
                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }
        }
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    int height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
    if (height < 10)
        height = 200;
    params.height = height;
    listView.setLayoutParams(params);
    listView.requestLayout();

}
private void setListViewHeight(可扩展listView listView,
国际集团){
ExpandableListAdapter listAdapter=(ExpandableListAdapter)listView.getExpandableListAdapter();
int totalHeight=0;
int desiredWidth=View.MeasureSpec.MakeMasureSpec(listView.getWidth(),
视图、测量(特别是精确);
对于(int i=0;i
你最好不要这样。嵌套的可滚动视图很可能会导致滚动问题。默认情况下,在两个可扩展列表适配器中保持第一个groupview展开。这非常好。但当我展开一个组时,它不会改变高度。
Add this line inside the scrollview:
 android:fillViewport="true"