简体   繁体   中英

Expandable list view listener does not work when activity is recreated

I have an expandable list view in a fragment and have set the list to expand only one row at a time in onGroupExpand function of the list. Everything works fine while activity is running. Once activity is resumed back from saved instance state the function onGroupExpand is never called. I tried to reset the listener for list but still its not working.

Below is the code for controlling the group expand function.

    private void setListExpandListener()
        {
            final ExpandableListView list = (ExpandableListView)getActivity().findViewById(android.R.id.list);
            list.setEmptyView(getView().findViewById(R.id.no_mixes_empty_list_view));
            list.setOnGroupExpandListener(new OnGroupExpandListener() 
            {   
                @Override
                public void onGroupExpand(int groupPosition) 
                { 
                    collapseGroupAtPosition(list, groupPosition);
                }
            });
        }



 private void collapseGroupAtPosition(ExpandableListView list,int position)
        {
            if (parentRowPosition != -1 && parentRowPosition != position) 
            {
                list.collapseGroup(parentRowPosition);  
            }
            parentRowPosition = position;
        }

I am trying to save the state of variable parentRowPosition and retrieve in restore state of fragment.

The retrieval works fine but the listener now does not work and all the rows of list can be expanded at a time.

Here is the code where listener is being set:-

    public void onViewCreated(View view, Bundle savedInstanceState)
        {
            mAdapter = new SavedTankExpandableListAdapter(getActivity());
            mAdapter.setDeleteListener(this);
            setListExpandListener();
            setListenersForViews();
            setOverlayAccordingToAppLaunchCount();
            super.onViewCreated(view, savedInstanceState);

        }

     public void onActivityCreated(Bundle savedInstanceState) 
            {
                super.onActivityCreated(savedInstanceState);
                onRestoreState(savedInstanceState);
            }

    public void onRestoreState(Bundle savedInstanceState)
{
    if(savedInstanceState==null)
        return;
    parentRowPosition=savedInstanceState.getInt("parentRowPosition",0);

    Utility.LHTLogs("Parent Row Position "+ parentRowPosition, true);
    if(mAdapter==null)
        mAdapter = new SavedTankExpandableListAdapter(getActivity());
    setListExpandListener();
}

Any help would be appreciated.

Where is the listener set and are you sure it is set again after restarting, in case you unplug the listener in OnStop or anywhere else? And a question like your's can't be answered like that without seeing full source code.

Excerpt from developer.google:

public void onViewCreated (View view, Bundle savedInstanceState) Added in API level 13

Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.

Parameters

view: The View returned by onCreateView(LayoutInflater, ViewGroup, Bundle).

savedInstanceState: If non-null, this fragment is being re-constructed from a previous saved state as given here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM