34

I would like to implement a ExpandableListView which should be expandable only after all the values have been set up within the adapter. Also I would like to be able to disable the collapsing of the expander.

Can I achieve this within an Android XML Layout?

Umair
  • 6,366
  • 15
  • 42
  • 50
Jabeer
  • 879
  • 1
  • 8
  • 13

2 Answers2

106

You can define a OnGroupClickListener which returns true, like so:

expandableList.setOnGroupClickListener(new OnGroupClickListener() {
  @Override
  public boolean onGroupClick(ExpandableListView parent, View v,
                              int groupPosition, long id) { 
    return true; // This way the expander cannot be collapsed
  }
});
Hauleth
  • 22,873
  • 4
  • 61
  • 112
추몽이
  • 1,076
  • 1
  • 8
  • 3
6

There is no way that I know of this to be done from xml.

You could add an OnGroupClickListener to the ExpandableListView, and consume its event if the group is already expanded:

myExpandableListView.setOnGroupClickListener(new OnGroupClickListener()
{
    @Override
    public boolean onGroupClick(ExpandableListView parent, 
        View v, int groupPosition, long id)
    {
        return parent.isGroupExpanded(groupPosition);
    }
});
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
  • Thank you very much. But i have made some changes to it and it worked for not collepse the list – Jabeer May 03 '11 at 15:10
  • Thank you very much. But i have made some changes to it and it worked for not collepse the listid.setOnGroupCollapseListener(new OnGroupCollapseListener() { public void onGroupCollapse(int id) { // TODO Auto-generated method stub ExpandableListView expand=(ExpandableListView) findViewById(R.id.expandableListView1); expand.expandGroup(id); } }); – Jabeer May 03 '11 at 15:10
  • hank you very much. But i have made some changes to it and it worked for not collepse the listid.setOnGroupCollapseListener(new OnGroupCollapseListener() { public void onGroupCollapse(int id) { // TODO Auto-generated method stub ExpandableListView expand=(ExpandableListView) findViewById(R.id.expandableListView1); expand.expandGroup(id); } }); – Jabeer May 03 '11 at 15:13
  • You should post your solution here, and you'll be able to accept it in 48 hours. You should do that, so this question to be closed. Please note though, that with your solution the list will collapse and right after that expand each group, so might be a lot waste. By consuming the click event you prevent the list from collapsing. – rekaszeru May 03 '11 at 15:51
  • I know that i am preventing the click event. But my requirement is in such a way. I have to expand my expandable list view all the time. SO i have the Prevent the Collapse event. Can you guide in expanding the ExpandableListview at the start itself. – Jabeer May 04 '11 at 07:02
  • You are not preventing, neither consuming the click event. You let it (and so the collapse to) happen, and correct the damage: expand the list again. – rekaszeru May 04 '11 at 11:04