在Android按钮上展开Expandable Listview Click

时间:2014-05-28 03:03:58

标签: android expandablelistview

我正在使用自定义适配器实现可扩展列表视图。 group元素需要有2个按钮,首先是父组元素,下面是按钮。

我的问题是,我想在点击下面的按钮而不是组元素时展开列表视图。此外,组元素onClick需要调用另一个活动。

我可以通过

禁用expandablelistview的扩展
mainExpListView.setOnGroupClickListener(new OnGroupClickListener() {

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

        return false;
    }

所以我的问题:

  1. 有没有办法禁用group元素并让它执行其他功能(比如导航到另一个活动?)

  2. 如何在底部图片上设置onclick方法以进行展开?

  3. enter image description here

2 个答案:

答案 0 :(得分:1)

 this is the groupview of my adapter and expand is the button on which i am performing  expand and collapse of my expanded list.



    public View getGroupView(final int groupPosition, final boolean isExpanded,
                             View convertView, final ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.childlist_group, null);
        }

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        ImageView expand = (ImageView) convertView.findViewById(R.id.expand);
        if (isExpanded) {
            expand.setImageResource(R.mipmap.ic_star_selected);
        } else {
            expand.setImageResource(R.mipmap.ic_keyboard_arrow_right_black_24dp);
        }
        expand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // frag.expandandcollapse(isExpanded,groupPosition);
                if (isExpanded) ((ExpandableListView) parent).collapseGroup(groupPosition);
                else ((ExpandableListView) parent).expandGroup(groupPosition, true);
            }
        });
        lblListHeader.setText(headerTitle);


        return convertView;
    }


therefore one more thing to keep in mind u have to override the default expand and collapse of your expandedlistview.........just change the return type of on groupclick listner from false to true


example
ExpandableListView.OnGroupClickListener()
 {
            @Override

            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) 
{


                String groupNvalue = MyApp.getInstance().categoryNvalues.get(groupPosition);
                Showtoast(groupNvalue);

                return true;///this has to be changed to true in your activity or fragment
            }
        });

答案 1 :(得分:0)

我不确定但是 在适配器中,您可以在视图上的单击侦听器上设置

@Override
public View getGroupView(int pos, boolean arg1, View convertView, ViewGroup arg3) {

if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.drawerlayout, null);
    }
      convertView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        }
    })
   }