gpt4 book ai didi

android - ScrollView 中的 ExpandableListView

转载 作者:太空宇宙 更新时间:2023-11-03 11:27:30 26 4
gpt4 key购买 nike

在我的应用程序中,我想制作一个页面,该页面具有 ExpandableListView 并在其下方有 CheckBox。

我的问题是我的 ExpandableListView 太大,使 CheckBox 超出了页面。

在较小的屏幕上根本不可见。

我试图将 ExpandableListView 放入 ScrollView 中,但它不起作用。

有没有办法制作我的设计?

这是我的代码和截图。

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White" >

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15sp"
android:text="In this page you can save a group of exercise under one routine."
android:textColor="@color/Black"
android:textSize="20sp" />

<ExpandableListView
android:id="@+id/instructionsExView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="15sp" >
</ExpandableListView>

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/instructionsExView"
android:layout_marginTop="15sp"
android:text="If you want to open the instruction again check &apos;need help?&apos;"
android:textColor="@color/Black"
android:textSize="20sp" />

<CheckBox
android:id="@+id/checkInstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="16dp"
android:checked="false"
android:text="dont show again when opening the page"
android:textColor="@color/Black" />

</RelativeLayout>
</ScrollView>

</RelativeLayout>

屏幕截图:

enter image description here

编辑:

enter image description here

最佳答案

在您的“onCreate”方法中写入:“setListViewHeight(expListView)”在“expListView.setAdapter(listAdapter)”之后,然后为您的可扩展 ListView 设置 OnGroupClickListener,如下所示:

expListView.setOnGroupClickListener(new OnGroupClickListener() {

@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
setListViewHeight(parent, groupPosition);
return false;
}
});

使用方法:

private void setListViewHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}

ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}


private void setListViewHeight(ExpandableListView listView,
int group) {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.EXACTLY);
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupItem = listAdapter.getGroupView(i, false, null, listView);
groupItem.measure(desiredWidth, 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, 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();

}

现在你可以开始了。

关于android - ScrollView 中的 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17696039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com