2017-10-23 21 views

答えて

0

複雑な拡張可能なビューで同様の問題が発生しました。 ExpandableListView.OnGroupClickListenerを作成する必要があります。

public class ExpandableListViewListener implements ExpandableListView.OnGroupClickListener { 

     private ExpandableListView expandableListView; 
     private Context context; 

    public ExpandableListViewListener(ExpandableListView expandableListView, Context context) { 
     this.expandableListView = expandableListView; 
     this.context = context; 
    } 

    /** 
    * Method that set the height of the expandableView 
    * @param listView to be expanded 
    * @param group index of the group tobe expanded 
    */ 
    private void setListViewExpanded(ExpandableListView listView, int group) { 

     int totalHeight = 0; 

     /* 
     * It checks GroupView height and, if expanded, relative children 
     * It sets final result as listview height. 
     * NOTE: it must be inserted into a card for a correct height calculation between different 
     * API versions 
     */ 
     ExpandableListAdapter listAdapter = listView.getExpandableListAdapter(); 
     int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.EXACTLY); 

     /* 
     * 2 nested cycles are performed: 
     * one for groupItems (headers) 
     * > one nested for childItems (when header is expanded) 
     */ 
     for (int i = 0; i < listAdapter.getGroupCount(); i++) { 

      View groupItem = listAdapter.getGroupView(i, false, null, listView); 
     groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); 

      //add current groupItem height to total count 
      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); 

        // add expanded child height 
        totalHeight += (int) context.getResources().getDimension(R.dimen.space_s) + listItem.getMeasuredHeight(); 

       } 
      } 
     } 

     //setting tot height 
     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalHeight; 
     listView.setLayoutParams(params); 
     listView.requestLayout(); 
    } 
}