简体   繁体   中英

Tab fragment containing ListView is blank on the second time it is selected

My app has a main activity that implements the TabListener class. In it, I can switch between 5 tabs. Here is how I manage this on my main activity:

@Override
protected void onCreate(Bundle p_SavedInstanceState) 
{
    super.onCreate(p_SavedInstanceState);
    setContentView(R.layout.main_fragment_activity);
    m_User = (User) getIntent().getExtras().getSerializable("User");

    if(m_ActionBar == null)
        m_ActionBar = getSupportActionBar();

    if(m_ActionBar.getTabCount() == 0) 
    {
        m_ActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab v_TabModules = m_ActionBar.newTab();
        v_TabModules.setText(" Modulos");
        v_TabModules.setIcon(getResources().getDrawable(R.drawable.ic_menu_copy));
        v_TabModules.setTabListener(this);//new NavigationTab(v_FragmentContainer, v_FragmentManager, new ModulesListFragment()));
        m_ActionBar.addTab(v_TabModules);

        ActionBar.Tab v_TabLocations = m_ActionBar.newTab();
        v_TabLocations.setText(" Comodos");
        v_TabLocations.setIcon(getResources().getDrawable(R.drawable.ic_menu_home));
        v_TabLocations.setTabListener(this);//new NavigationTab(v_FragmentContainer, v_FragmentManager, new LocationsListFragment()));
        m_ActionBar.addTab(v_TabLocations);

        ActionBar.Tab v_TabScenes = m_ActionBar.newTab();
        v_TabScenes.setText("Cenas");
        v_TabScenes.setIcon(getResources().getDrawable(R.drawable.ic_menu_crop));
        v_TabScenes.setTabListener(this);//new NavigationTab(v_FragmentContainer, v_FragmentManager, new ScenesListFragment()));
        m_ActionBar.addTab(v_TabScenes);

        ActionBar.Tab v_TabSchedules = m_ActionBar.newTab();
        v_TabSchedules.setText("Agendamentos");
        v_TabSchedules.setIcon(getResources().getDrawable(R.drawable.ic_menu_schedule));
        v_TabSchedules.setTabListener(this);//new NavigationTab(v_FragmentContainer, v_FragmentManager, new ScheduleListFragment()));
        m_ActionBar.addTab(v_TabSchedules);

        ActionBar.Tab v_TabUser = m_ActionBar.newTab();
        v_TabUser.setText("Usuario");
        v_TabUser.setIcon(getResources().getDrawable(R.drawable.ic_menu_user));
        v_TabUser.setTabListener(this);//new NavigationTab(v_FragmentContainer, v_FragmentManager, new UserFragment()));
        m_ActionBar.addTab(v_TabUser);
    }

    if(p_SavedInstanceState != null) 
    {
        int v_IndexTab = p_SavedInstanceState.getInt("tabIndex");
        m_ActionBar.setSelectedNavigationItem(v_IndexTab);
    }
    else
    {
        m_ActionBar.setSelectedNavigationItem(0);
    }
}

@Override
public void onTabSelected(Tab p_Tab, FragmentTransaction arg1) 
{
    BaseClassGafitFragment v_Fragment = null;
    if(p_Tab.getPosition() == 0) 
        v_Fragment = new ModulesListFragment();
    else if(p_Tab.getPosition() == 1) 
        v_Fragment = new LocationsListFragment();
    else if(p_Tab.getPosition() == 2) 
        v_Fragment = new ScenesListFragment();
    else if(p_Tab.getPosition() == 3) 
        v_Fragment = new ScheduleListFragment();
    else if(p_Tab.getPosition() == 4) 
        v_Fragment = new UserFragment();
    if(v_Fragment != null)
        getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_activity, v_Fragment).commit();
}

The ModulesListFragment, LocationsListFragment, ScenesListFragment and ScheduleListFragment classes exends the Fragment class and have a ListView inside. Here is an example of how the ModulesListFragment manages this list:

@Override
public View onCreateView(LayoutInflater p_Inflater, ViewGroup p_Container,
        Bundle p_SavedInstanceState) 
{

    View v_View = p_Inflater.inflate(R.layout.moduleslist_activity, p_Container);
    m_ListView = (ListView) v_View.findViewById(R.id.modules_list_id_list_view);
    m_ListView.setOnItemClickListener(onItemClick());

    return super.onCreateView(p_Inflater, p_Container, p_SavedInstanceState);
}

@Override
public void onResume() 
{
    super.onResume();
    loadModulesFromCache();
}

private void loadModulesFromCache()
{
    ArrayList<AbstractAction> v_ModulesList = new ArrayList<AbstractAction>();
    for (Module v_Module : SocketHandlerService.m_Cache.getModulesList())
        v_ModulesList.add(v_Module);
    for (Waiting v_Waiting : SocketHandlerService.m_Cache.getWaitingList())
        v_ModulesList.add(v_Waiting);
    ActionListAdapter v_Adapter = new ActionListAdapter(getActivity().getLayoutInflater(), v_ModulesList);
    m_ListView.setAdapter(v_Adapter);
}

And here is how my ActionListAdapter, that extends the BaseAdapter class, overrides the getView method:

@Override
public View getView(int p_Position, View p_ConvertView, ViewGroup p_Parent)
{
    ViewHolder v_Holder = new ViewHolder();
    if (p_ConvertView == null)
    {
        p_ConvertView = m_Inflater.inflate(R.layout.module_list_item_module_view, p_Parent, false);
        AbstractAction v_Action = m_ActionList.get(p_Position);
        v_Holder.m_Name = (TextView) p_ConvertView.findViewById(R.id.module_list_item_view_name);
        v_Holder.m_Name.setText(v_Action.getDescription());
        v_Holder.m_Name.setTextColor(Color.WHITE);
        v_Holder.m_Image = (ImageView) p_ConvertView.findViewById(R.id.module_list_item_view_photo);
        v_Holder.m_Image.setImageResource(isRed(p_Position) ? ((AbstractModule)v_Action).getImageDrawableRed() : v_Action.getImageDrawable());
        if (v_Action instanceof Module)
        {
            v_Holder.m_Location = (TextView) p_ConvertView.findViewById(R.id.module_list_item_view_location);
            Location v_Location = SocketHandlerService.m_Cache.getSpecificLocation(((Module) v_Action).getLocationId());
            if (v_Location != null)
            {
                v_Holder.m_Location.setText(v_Location.m_Description);
                v_Holder.m_Location.setTextColor(Color.WHITE);
            }
        }
        p_ConvertView.setTag(v_Holder);
    }
    else 
    {
        v_Holder = (ViewHolder) p_ConvertView.getTag();
    }
    return p_ConvertView;
}

The application was first built using one activity for each of the five classes that now are fragments, and now I'm migrating it to the fragment implementation for the sake of using tabs. The app is running ok, but when I change from one tab to the other and then back to the previous one the listView is just gone! The last tab, that does not have a listView, works ok but the other 4 ones are only working for the first time they are selected. Have anyone got an idea of what may be happening? I found some people with similar problems here and here but they're implementation seems to be different than mine, and since I'm beggining to deal with fragments now, I have no hability to make the relation between what solved the problem in they're code and my problem... Any help is highly appreciated and any futher information needed will be promptly provided! Thanks in advance to everyone that is even reading this!

Changing

@Override
public View onCreateView(LayoutInflater p_Inflater, ViewGroup p_Container,
        Bundle p_SavedInstanceState) 
{

    View v_View = p_Inflater.inflate(R.layout.moduleslist_activity, p_Container);
    m_ListView = (ListView) v_View.findViewById(R.id.modules_list_id_list_view);
    m_ListView.setOnItemClickListener(onItemClick());

    return super.onCreateView(p_Inflater, p_Container, p_SavedInstanceState);
}

for

@Override
public View onCreateView(LayoutInflater p_Inflater, ViewGroup p_Container,
        Bundle p_SavedInstanceState) 
{

    View v_View = p_Inflater.inflate(R.layout.moduleslist_activity, p_Container);
    m_ListView = (ListView) v_View.findViewById(R.id.modules_list_id_list_view);
    m_ListView.setOnItemClickListener(onItemClick());

    return v_View;
}

And

@Override
public void onTabSelected(Tab p_Tab, FragmentTransaction arg1) 
{
    BaseClassGafitFragment v_Fragment = null;
    if(p_Tab.getPosition() == 0) 
        v_Fragment = new ModulesListFragment();
    else if(p_Tab.getPosition() == 1) 
        v_Fragment = new LocationsListFragment();
    else if(p_Tab.getPosition() == 2) 
        v_Fragment = new ScenesListFragment();
    else if(p_Tab.getPosition() == 3) 
        v_Fragment = new ScheduleListFragment();
    else if(p_Tab.getPosition() == 4) 
        v_Fragment = new UserFragment();
    if(v_Fragment != null)
        getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_activity, v_Fragment).commit();
}

for

public void onTabSelected(Tab p_Tab, FragmentTransaction p_FragmentTransaction) 
{
    BaseClassGafitFragment v_Fragment = null;
    if(p_Tab.getPosition() == 0) 
        v_Fragment = new ModulesListFragment();
    else if(p_Tab.getPosition() == 1) 
        v_Fragment = new LocationsListFragment();
    else if(p_Tab.getPosition() == 2) 
        v_Fragment = new ScenesListFragment();
    else if(p_Tab.getPosition() == 3) 
        v_Fragment = new ScheduleListFragment();
    else if(p_Tab.getPosition() == 4) 
        v_Fragment = new UserFragment();
    if(v_Fragment != null)
        p_FragmentTransaction.replace(R.id.main_fragment_activity, v_Fragment);
}

Made our code work. Thank you so much @Luksprog!

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