2013-07-17 2 views
3

내 앱에서 ExpandableListView가 있고 그 아래에 CheckBox가있는 페이지를 만들고 싶습니다.ExpandableList 스크롤보기 내에서보기

제 문제는 제 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

당신은 스크롤 뷰 내부 캔트 사용 목록보기, 당신이 사용해야 있도록 스크롤 뷰를보고보기로 추가 동적으로

1

expandablelistview는 parentTop 및 parentBottom이있는 두 항목 사이에 있어야하므로 RelativeLayout은 두 개 이상 필요하지 않으므로 나머지는 위에/이하.

<?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

다음과 같은 코드를 사용하여이 문제를 해결할 수 있습니다. 나는 50을 listitem 값을 위의 코드를 확인하고 매우 작은 디스플레이 장치에서 테스트 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" /> 

당신이 대체 할 수 ListView을 사용하고 잘 작동합니다.

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

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

나는 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; 
     } 
    }); 

사용 방법 : 당신 "에서 onCreate"메소드 쓰기에서

+1

작동하지 않습니다. ExpandableListView가 확장되지 않습니다. – dasdasd

+0

http://scackoverflow.com/questions/34269075/scrollview-not-working-androidinside-the-scrollview-i-have-a-expandable-listvie이 링크를 참조하고 해결책을주십시오 @DASDASD – YUVRAJ

17

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

이것은 매우 효과적입니다. me ... 그러나 ExpandableListView가 스크롤 업되어보기 영역을 벗어나면 목록이 자동으로 축소됩니다. 이것에 대한 해결책은 무엇일까요? _setListViewHeight (ExpandableListView ... _ method – user1036908

+0

)이 코드는 ExpandableListView 속성이 layout_height = "wrap_content"일 필요가있을 때 유용합니다. 코드를 사용하면보기가 내용을 바꿔 쓸 것입니다 .. – Sniper

+0

두 번째 기능은 정상적으로 작동하지만 첫 번째 기능은 정상적으로 작동합니다. 하나의 그룹이 클릭하기 전에 즉, expandablelistview의 높이가 올바르지 않습니다. 누군가가 이것을 해결할 수 있었습니까? – dvc

6

@Dmila 램answer에 기초하여, 나는 그것을 할 수 있었다. 내 경우

, 나는 초기화하고 그래서 같이 onCreate 방법에 ExpandableListView을 채울 : 나는 각 그룹을 확장

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

Nice.It는 매우 유용하고 정확합니다. –