2013-07-17 129 views
3

在我的应用程序中,我想制作一个具有ExpandableListView并位于他下面的CheckBox的页面。ExpandableListView在滚动视图内

我的问题是,我的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

+0

使用'MergeAdapter':https://github.com/commonsguy/cwac-merge – gunar

回答

1

你滚动视图中不能使用的ListView,所以你应该使用滚动查看并添加行作为视图动态。

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> 
+0

但我想我的复选框在下面ExpandableListView – dasdasd

+0

它是,看看复选框内的parentBottom。接下来的项目将被着色复选框,查看下一个项目的属性。因为你最后看到ExpandableListView并不意味着它是最后一个显示在屏幕上。 – AlexBcn

+0

它的某种工作。由于ExpandableListView现在只在小面积内扩展。我希望它会扩大到屏幕尺寸。看我的编辑和屏幕截图。 – dasdasd

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列表项的值检查上面的代码和一个非常小的显示设备上进行测试,它工作正常。

<?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检查,这对我工作的罚款。 “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; 
     } 
    }); 

使用的方法:

+1

它不工作。 ExpandableListView不会展开。 – dasdasd

+0

http://stackoverflow.com/questions/34269075/scrollview-not-working-androidinside-the-scrollview-i-have-a-expandable-listvie参考此链接并给出解决方案请@dasdasd – YUVRAJ

17
在你 “的onCreate” 方法写入

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

    } 

现在你很好走。

+0

这工作得很好我......但是,当ExpandableListView向上滚动并离开查看区域时,列表会自动折回。有什么可以解决这个问题的? 我正在使用_setListViewHeight(ExpandableListView ..._方法 – user1036908

+0

此代码是有用的时,ExpandableListView需要有属性layout_height =“wrap_content”。您的视图将代码包装内容一直正确 – Sniper

+0

第二个函数工作正常,但第一个@dvc是否在“expListView.setAdapter(listAdapter)”之后使用了“setListViewHeight(expListView)”?是否有人能够解决这个问题? – dvc

6

基于@Dmila拉姆单曲answer,我能做到这一点。

就我而言,我初始化和填充ExpandableListViewonCreate方法,像这样:

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

这几乎是它,然后它工作正常,但这种解决方案甚至可以优化进一步。

+0

不错。它非常有帮助和准确。 –

相关问题