2013-07-17 177 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

不錯。它非常有幫助和準確。 –

相關問題