检测ExpandableListView组单击

时间:2011-05-18 14:08:04

标签: android expandablelistview

我有一个自定义的ExpandableListView elv,我能够检测到使用下面的代码点击Child的时间。

    elv.setOnChildClickListener(new OnChildClickListener() {
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                int childPosition, long id) {

但是,当我更改它以尝试检测何时单击某个组项时,这不起作用。有谁知道为什么?

Eclipse给了我错误:

  

方法   setOnGroupClickListener(ExpandableListView.OnGroupClickListener)   在ExpandableListView类型中没有   适用于论点(新的   OnGroupClickListener(){})

    elv.setOnGroupClickListener(new OnGroupClickListener() {
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition,
                long id) {

3 个答案:

答案 0 :(得分:7)

确保您已导入android.widget.ExpandableListView.OnGroupClickListener或替换

  

new OnGroupClickListener(){

new android.widget.ExpandableListView.OnGroupClickListener {

答案 1 :(得分:0)

这些方法也可以在ExpandableListAdapter内使用     public void onGroupExpanded(int groupPosition){}
    public void onGroupCollapsed(int groupPosition){}

答案 2 :(得分:0)

private ExpandableListView listView;


listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

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

                      //get header
            TextView lbListenerHeader = parent.findViewById(R.id.lbListHeader);
            String data1= lbListenerHeader.getText().toString();
            Toast.makeText(getContext(),data1 ,Toast.LENGTH_SHORT).show();

                    //get list item
            TextView tv= (TextView) v.findViewById(R.id.lbListItem);
            ExpandableListView elv= (ExpandableListView) parent.findViewById(R.id.lvExp);
            String data= tv.getText().toString();
            Toast.makeText(getContext(),data ,Toast.LENGTH_SHORT).show();

            return true;
        }

    });