das*_*asd 3 xml android android-layout android-xml
在我的应用程序中,我想创建一个具有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 'need help?'"
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>
Run Code Online (Sandbox Code Playgroud)
截屏:

编辑:

Dmi*_*Ram 18
在你的"onCreate"方法中写入:"setListViewHeight(expListView)"之后的"expListView.setAdapter(listAdapter)"然后为你的可扩展列表视图设置OnGroupClickListener,如下所示:
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
setListViewHeight(parent, groupPosition);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
使用方法:
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();
}
Run Code Online (Sandbox Code Playgroud)
现在你很高兴.
根据@ Dmila Ram的回答,我能够做到.
就我而言,我初始化和填充ExpandableListView在onCreate像这样的方法:
expandableListView = (ExpandableListView) findViewById(R.id.appsMenu);
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expandableListView.setAdapter(listAdapter);
for (int i = 0; i < listAdapter.getGroupCount(); i++)
expandableListView.expandGroup(i);
setListViewHeight(expandableListView);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
setListViewHeight(parent, groupPosition);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
请注意,我扩展了每个组.之后我打电话setListViewHeight(expandableListView);
此方法负责计算ExpandableListView第一次创建时的高度.
这是代码:
private void setListViewHeight(ExpandableListView listView) {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupView = listAdapter.getGroupView(i, true, null, listView);
groupView.measure(0, View.MeasureSpec.UNSPECIFIED);
totalHeight += groupView.getMeasuredHeight();
if (listView.isGroupExpanded(i)){
for(int j = 0; j < listAdapter.getChildrenCount(i); j++){
View listItem = listAdapter.getChildView(i, j, false, null, listView);
listItem.measure(0, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Run Code Online (Sandbox Code Playgroud)
实际上没有必要扩展组,而是我们可以循环遍历子项并将它们添加到总高度的计算中.
这将使所有小部件都可ScrollView滚动,并且它也将ExpandableListView正确显示.
然后我们还需要这个方法,以确保点击组也将计算高度:
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();
}
Run Code Online (Sandbox Code Playgroud)
这就是它,然后它工作正常,但这个解决方案可以进一步优化.
| 归档时间: |
|
| 查看次数: |
11851 次 |
| 最近记录: |