Code example for ExpandableListView

Methods: getChildAtexpandGroupgetFlatListPositionsetSelectedChildsmoothScrollToPosition, getFirstVisiblePosition, getLastVisiblePosition

0
   * @param childPosition 
   */ 
  void selectCurrent(ExpandableListView list, int groupPosition, int childPosition) {
    if (groupPosition != INVALID_ROW_ID && childPosition != INVALID_ROW_ID) {
      long packedPosition = ExpandableListView.getPackedPositionForChild(groupPosition, childPosition);
      int flatPosition = list.getFlatListPosition(packedPosition);
      expandGroup(list, groupPosition);
      list.setSelectedChild(groupPosition, childPosition, true);
      list.smoothScrollToPosition(flatPosition);
      selectCurrent(list, flatPosition);
      return; 
    } 
    if (groupPosition != INVALID_ROW_ID && childPosition == INVALID_ROW_ID) {
      long packedPosition = ExpandableListView.getPackedPositionForGroup(groupPosition);
      int flatPosition = list.getFlatListPosition(packedPosition);
      expandGroup(list, groupPosition);
      list.smoothScrollToPosition(flatPosition);
      selectCurrent(list, flatPosition);
    } 
  } 
 
  /** 
   * Convenience method to flag that we're expanding the list in software, not 
   * actually clicking it. 
   *  
   * @param list 
   * @param groupPosition 
   */ 
  private void expandGroup(ExpandableListView list, int groupPosition) {
    setIgnoreExpand(true); 
    list.expandGroup(groupPosition);
    setIgnoreExpand(false); 
  } 
 
  /** 
   * @param list 
   * @param flatPosition 
   */ 
  void selectCurrent(ExpandableListView list, int flatPosition) {
    if (flatPosition >= list.getFirstVisiblePosition() && flatPosition <= list.getLastVisiblePosition()) {
      View view = list.getChildAt(flatPosition - list.getFirstVisiblePosition());
      if (view != null && view instanceof ListItemView) {
        ListItemView itemView = (ListItemView) view;
        itemView.setCurrent(true);
      } 
    }