scrollview中的ExpandableListView

时间:2013-07-17 09:27:01

标签: android xml 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 &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

5 个答案:

答案 0 :(得分:17)

在你的“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;
        }
    });

使用过的方法:

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();

    }
现在你很高兴。

答案 1 :(得分:7)

基于@ Dmila Ram answer,我能够做到。

就我而言,我在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;
        }
     });

请注意,我展开了每个组。之后我拨打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(); } 的高度。

以下是代码:

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(); }

然后我们还需要这种方法,以确保点击组也会计算高度:

        MyCommand.CommandText = "select *  from Quotient_M m join Quotient_D q on m.q_id = q.q_id   , Company c where m.Q_id = 5"
        MyCommand.CommandType = CommandType.Text
        myDA.SelectCommand = MyCommand
        myDA.Fill(myDS, "Quotient_M")
        myDA.Fill(myDS, "Quotient_D")
        myDA.Fill(myDS, "Customer")


        Dim cr As New CrystalReport3()
        cr.SetDataSource(myDS)
        Dim rpt1 As New Report_Quotation()
        rpt1.CrystalReportViewer1.ReportSource = cr
        rpt1.Show()

这就是它,然后它工作正常,但这个解决方案可以进一步优化。

答案 2 :(得分:1)

你不能在滚动视图中使用listview,所以你应该使用滚动视图并动态地将行添加为视图。

答案 3 :(得分:1)

你不需要多个RelativeLayout,因为expandablelistview应该在两个项目之间(在你的情况下),这两个项目将是第一个有parentTop和parentBottom的项目,其余项目将定位在上面/下面

<?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"
android:orientation="vertical" >

<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" />

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

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/checkInstructions"
    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" />

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

</RelativeLayout>

答案 4 :(得分:0)

您可以使用以下代码来实现此功能。我使用了ListView您可以使用ExpandableListView

替换
<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_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/WHITE"
    android:textSize="20sp" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2" >

</ListView>

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

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

我用大约50个listitem值检查了上面的代码,并在一个非常小的显示设备上进行了测试,它运行正常。

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_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/WHITE"
    android:textSize="20sp" />

<ExpandableListView
    android:id="@+id/expandableListView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2" >

</ExpandableListView>

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

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

我使用ExpandableListView进行了检查,它对我来说很合适。