2011-09-03 74 views
0

我在设置OnClickListener列出ExpandableListActivity中的项目时遇到了问题。代码结构相匹配下面的例子:Android扩展列表 - OnClickListener

ExpandableListAdapter mAdapter; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Assign the adapter 
    mAdapter = new MyExpandableListAdapter(); 
    setListAdapter(mAdapter); 
} 

public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
    private String[] groups = { "foo", "bar"}; 
    private String[][] children = { 
       {"fooA", "barA"}, 
     {"fooB", "barB"} 
      }; 

    public Object getChild(int groupPosition, int childPosition) { 
     return children[groupPosition][childPosition]; 
    } 

    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    public int getChildrenCount(int groupPosition) { 
     return children[groupPosition].length; 
    } 

    public TextView getGenericView() { 
     // Layout parameters for the ExpandableListView 
     AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
       ViewGroup.LayoutParams.MATCH_PARENT, 64); 

     TextView textView = new TextView(Calculations.this); 
     textView.setLayoutParams(lp); 
     // Center the text vertically 
     textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
     // Set the text starting position 
     textView.setPadding(64, 0, 0, 0); 
     return textView; 
    } 

     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, 
      View convertView, ViewGroup parent) { 
     TextView textView = getGenericView(); 
     textView.setText(getChild(groupPosition, childPosition).toString()); 
     textView.setPadding(98, 0, 0, 0); 
     return textView; 
    } 

    public Object getGroup(int groupPosition) { 
     return groups[groupPosition]; 
    } 

    public int getGroupCount() { 
     return groups.length; 
    } 

    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
      ViewGroup parent) { 
     TextView textView = getGenericView(); 
     textView.setText(getGroup(groupPosition).toString()); 
     return textView; 
    } 

    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

    public boolean hasStableIds() { 
     return true; 
    } 
} 

一旦我能够设置监听器,我将最有可能做这样的事情:

// Listener 
{ 
    switch(groupPosition) { 
    case 0: 
     switch(childPosition) { 
     case 0: 
      selected = 00; 
      return true; 
     } 
    } 
}// Close listener 

请告诉我,如果我不足够清楚。

+0

你想附加听众的位置?给ExpandableList中的组的孩子? – momo

回答

0

您应该在您的活动中覆盖onChildClick()

布尔onChildClick(ExpandableListView父母,视图V,INT groupPosition,INT childPosition,长ID) 重写这个接收回调孩子被点击时。

+0

这是否有帮助? – Ronnie