如何在没有子项的expandablelistview组上添加click事件

时间:2016-05-05 12:07:26

标签: android expandablelistview

在我的expandablelistview中,有些人有孩子,有些则没有孩子。没有子节点的GroupView包含Textview和Button。单击该按钮,我需要通过intent将textview的数据传递给下一个活动。现在我的问题是,我无法在groupview的按钮上设置onclick事件,该按钮没有子节点,我也需要groupPosition。

这是我在主java文件中的clickListener代码:

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            switch (groupPosition) {
                case 0:
                    Log.d(">>>", "" + groupPosition);
                    break;
                case 1:
                    Log.d(">>>", "" + groupPosition);
                    break;
                case 2:
                    Log.d(">>>", "" + groupPosition);
                    break;
                case 3:
                    Log.d(">>>", "" + groupPosition);
                    break;
            }
            return false;
        }
    });

这是我的适配器代码:

public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    groupIndex = groupPosition;
    QuotedListGroup group = (QuotedListGroup) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.activity_expandable_listview, null);
    }

    if(groupPosition%2 == 0) {
        convertView.findViewById(R.id.part_1).setVisibility(View.VISIBLE);
        convertView.findViewById(R.id.part_2).setVisibility(View.GONE);
        //Other code..
    } else {
        convertView.findViewById(R.id.part_1).setVisibility(View.GONE);
        convertView.findViewById(R.id.part_2).setVisibility(View.VISIBLE);
        //Other code..
    }
    return convertView;
}

实际上,我需要在同一个expandablelistview中为偶数和奇数组显示两种不同的布局。

非常感谢您提供的任何帮助!

2 个答案:

答案 0 :(得分:1)

像这样

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

            Bundle bundle;
            switch (groupPosition) {
               case 0:
               //add your code here

            return false;
        }
    });

答案 1 :(得分:0)

使用 ExpandableListView.OnGroupClickListener()它还会为您提供组位置。