views:

144

answers:

0

I have an Activity with a few Tabs created dynamically within a loop, where each tab contains an ExpandableListView. I then register the OnGroupExpandedListener to each ExpandableListView within each tab, to have a behaviour that only one group can be expanded at any one given time. The problem arises when i switch to another tab, the previous selected tab changes the behaviour and allows multiple groups to be expanded at the same time. This is the code i'm using to achieve that.

TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
for (int x = 0; x < items.size(); x++) {
    final DataItem item = DataItem.get(x);
    tabHost.addTab(createTab(tabHost, item));

}

private TabSpec createTab(TabHost mTab, final DataItem item) {

        return mTab.newTabSpec(item.getName())
                .setIndicator(item.getName())
                .setContent(new TabContentFactory() {
                    @Override
                    public View createTabContent(String tag) {

                        List<Category> mCategories = item
                                .getCategories();

                        HashMap<String, String> dataStore = new HashMap<String, String>();

                        final CustomBaseExpandableListAdapter mAdapter = new CustomBaseExpandableListAdapter(
                                ObservationActivityView.this,
                                R.layout.template_view, item,
                                mCategories, dataStore,
                                ObservationActivityView.this);

                        mExpandableList = new ExpandableListView(
                                ObservationActivityView.this);
                        mExpandableList.setAdapter(mAdapter);


                                .setOnGroupExpandListener(new OnGroupExpandListener() {

                                    @Override
                                    public void onGroupExpand(int groupPosition) {
                                        Log.i("OnGroupExpandListener : onGroupExpand",
                                                "" + groupPosition);

                                        // Collapse all other groups when one is
                                        // expanded
                                        int grpCount = mAdapter.getGroupCount();
                                        for (int i = 0; i < grpCount; ++i) {
                                            if (i == groupPosition) {
                                                continue;
                                            } else if (mExpandableList
                                                    .isGroupExpanded(i)) {
                                                // if the group is already
                                                // expanded, collapse it
                                                mExpandableList
                                                        .collapseGroup(i);
                                            }
                                        }

                                        // TODO
                                        int childrenCount = mAdapter
                                                .getChildrenCount(groupPosition);
                                        for (int x = 0; x < childrenCount; x++) {
                                            mAdapter.getChildId(groupPosition,
                                                    x);
                                            // Log.i("OnGroupExpandListener : onGroupExpand",
                                            // "Child ID "
                                            // +
                                            // mAdapter.getChildId(groupPosition,
                                            // x));
                                            Log.i("OnGroupExpandListener : onGroupExpand",
                                                    "Child Unique ID "
                                                            + mAdapter
                                                                    .getChildUniqueId(
                                                                            groupPosition,
                                                                            x));

                                        }

                                    }
                                });
                        return mExpandableList;
                    }

                });

    }