Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android expandablelistview, expand the view only on click of group indicator, open new activity if clicked any where but groupindicator

I have a ExpandableListView on my Activity, where group contains image and name, while child contains variant of group (name, id etc). Regardless I click on the expand/collapse button, or on the any where in group, it expands/collapses always. What I want, the list to expand/collapse only if user clicks on the indicator. How can I do this? For clicking on the TextView, I want to open the different activity. Unfortunately OnGroupClickListener does not provide this info.

Can anyone help me on this?

Thanks

like image 562
Devesh Agrawal Avatar asked Nov 29 '15 05:11

Devesh Agrawal


1 Answers

You can do that by doing some modification in your custom Adapter getGroupView method and adding two more method in your custom Adapter which will be called depending on the view clicked. Here I am posting an example code:

    //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;
    }

And now from the Activity you can call the Custom Adapter like as below code which will override those two methods which we have added in Custom Adapter

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);
like image 100
Pankaj Avatar answered Sep 27 '22 18:09

Pankaj