Android expandablelistview, expand the view only by clicking on the group indicator, open a new activity if you click anywhere except the group pointer

I have an ExpandableListView in my activity, where the group contains the image and name, and the child has a group option (name, identifier, etc.). Regardless of the fact that I click the expand / collapse button or anywhere in the group, it always expands / collapses. I want the list to expand / collapse only if the user clicks on the indicator. How can i do this? To click on a TextView, I want to open a different activity. Unfortunately, OnGroupClickListener does not provide this information.

Can someone help me with this?

thanks

+4
source share
2 answers

You can do this by making some changes to your custom adapter getGroupView and adding two more methods to the custom adapter , which will be called depending on the kind of clicked. Here I am posting a sample 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 Activity you can call the Custom Adapter , as shown below, which will override the two methods that we added to the 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);
+7
source

Just set setOnGroupClickListener()to your ExpandableListView and return true, Like:

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

this will disable the group click and then set the onclicklistener () on which you want to expand the group.

-1
source

Source: https://habr.com/ru/post/1617872/


All Articles