2012-01-19 54 views
0

我具有extands BaseExpandableListAdapter的ExpandableListView。安卓:如何讓孩子單擊ExpandableListView

我想點擊任何一個孩子時,一個視圖添加到孩子。

我怎樣才能獲得被點擊時,一個孩子的正確位置? (或者我怎樣才能給它增加一個孩子)。

+0

這是爲'onChildClick(ExpandableListView父母,視圖V,INT groupPosition,INT childPosition,長期ID爲明顯)' – st0le

+0

一組具有 「isExpanded」 布爾變量\t 「@覆蓋 \t公共視圖getChildView(INT groupPosition,INT childPosition, \t \t \t布爾isLastChild,查看convertView,父的ViewGroup){}」方法..有兒童鑑於任何varible也 –

+0

ExapandableListview是隻有1級可擴展的,你必須實現自己的NestedExpandableListview ... – st0le

回答

1
public void onCreate(Bundle savedInstanceState) { 
     try{ 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

     SimpleExpandableListAdapter expListAdapter = 
      new SimpleExpandableListAdapter(
        this, 
        createGroupList(),    // Creating group List. 
        R.layout.group_row,    // Group item layout XML.   
        new String[] { "Group Item" }, // the key of group item. 
        new int[] { R.id.row_name }, // ID of each group item.-Data under the key goes into this TextView.     
        createChildList(),    // childData describes second-level entries. 
        R.layout.child_row,    // Layout for sub-level entries(second level). 
        new String[] {"Sub Item"},  // Keys in childData maps to display. 
        new int[] { R.id.grp_child}  // Data under the keys above go into these TextViews. 
       ); 
      setListAdapter(expListAdapter);  // setting the adapter in the list. 

     }catch(Exception e){ 
      System.out.println("Errrr +++ " + e.getMessage()); 
     } 
    } 

    /* Creating the Hashmap for the row */ 
    @SuppressWarnings("unchecked") 
    private List createGroupList() { 
      ArrayList result = new ArrayList(); 
      for(int i = 0 ; i < 15 ; ++i) { // 15 groups........ 
      HashMap m = new HashMap(); 
      m.put("Group Item","Group Item " + i); // the key and it's value. 
      result.add(m); 
      } 
      return (List)result; 
    } 

    /* creatin the HashMap for the children */ 
    @SuppressWarnings("unchecked") 
    private List createChildList() { 

     ArrayList result = new ArrayList(); 
     for(int i = 0 ; i < 15 ; ++i) { // this -15 is the number of groups(Here it's fifteen) 
      /* each group need each HashMap-Here for each group we have 3 subgroups */ 
      ArrayList secList = new ArrayList(); 
      for(int n = 0 ; n < 3 ; n++) { 
      HashMap child = new HashMap(); 
      child.put("Sub Item", "Sub Item " + n);   
      secList.add(child); 
      } 
     result.add(secList); 
     }   
     return result; 
    } 
    public void onContentChanged () { 
     System.out.println("onContentChanged"); 
     super.onContentChanged();   
    } 
    /* This function is called on each child click */ 
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { 
     System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition); 
     return true; 
    } 

    /* This function is called on expansion of the group */ 
    public void onGroupExpand (int groupPosition) { 
     try{ 
      System.out.println("Group exapanding Listener => groupPosition = " + groupPosition); 
     }catch(Exception e){ 
      System.out.println(" groupPosition Errrr +++ " + e.getMessage()); 
     } 
    } 
0

對於人們仍在尋找這個問題,我找到了答案是:

listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 

      ExpandableListAdapter eListAdapter = (ExpandableListAdapter)parent.getExpandableListAdapter(); 
      ListItem item = (ListItem)eListAdapter.getChild(groupPosition, childPosition); 
      return true; 
     } 
    }); 
相關問題