android expandablelistview,只在单击组指示器时展开视图,如果点击任何地方,则打开新活动,但groupindicator

时间:2015-11-29 05:28:35

标签: android expandablelistview

我的Activity上有一个ExpandableListView,其中group包含图像和名称,而child包含group的变体(name,id等)。无论我单击展开/折叠按钮,还是单击组中的任何位置,它都会一直展开/折叠。我想要的是,只有当用户点击指标时,才会展开/折叠列表。我怎样才能做到这一点?为了单击TextView,我想打开不同的活动。不幸的是,OnGroupClickListener不提供此信息。

有人可以帮我吗?

由于

2 个答案:

答案 0 :(得分:7)

您可以通过在自定义适配器 getGroupView方法中进行一些修改,然后在自定义适配器中添加另外两种方法来执行此操作,具体取决于单击视图。这里我发布一个示例代码:

    //Add these two methods in your Custom Adapter 
    public void OnIndicatorClick(boolean isExpanded, int position){

     }

    public void OnTextClick(){

    }   

    //this is the getGroupView code
    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded,
            View convertView, 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.list_group, null);
        }



        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);

        lblListHeader.setText(headerTitle);

        lblListHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(_context, "text Clicked", Toast.LENGTH_LONG).show();

            }
        });

        ImageView indicator = (ImageView) convertView.findViewById(R.id.imageview_list_group_indicator);
        indicator.setSelected(isExpanded);
        indicator.setTag(groupPosition);

        indicator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (Integer)v.getTag();
                Toast.makeText(_context, "Indicator Clicked", Toast.LENGTH_LONG).show();
                OnIndicatorClick(isExpanded,position);

            }
        });
        return convertView;
    }

现在,从活动,您可以调用自定义适配器,如下面的代码,它将覆盖我们在自定义适配器中添加的两种方法

ExpandableListAdapter listAdapter = new ExpandableListAdapter(MainActivity.this,
                    listDataHeader, childDataList){
       @Override
       public void OnIndicatorClick(boolean isExpanded, int position) {
          if(isExpanded){
             expandableListView.collapseGroup(position);
          }else{
             expandableListView.expandGroup(position);
          }
      }

      @Override
      public void OnTextClick() {
          //Do whatever you want to do on text click
      }
 };

 expandableListView.setAdapter(listAdapter);

答案 1 :(得分:-1)

只需将setOnGroupClickListener()设置为您的ExpandableListView并返回true,Like:

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

这将禁用组的单击,然后设置要在其上展开组的onclicklistener()。