包含ListView的选项卡片段在第二次被选择时为空白 [英] Tab fragment containing ListView is blank on the second time it is selected

查看:65
本文介绍了包含ListView的选项卡片段在第二次被选择时为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有实现TabListener类的主要活动.在其中,我可以在5个标签之间切换.这是我在主要活动中如何处理的方法:

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();
}

ModulesListFragment,LocationsListFragment,ScenesListFragment和ScheduleListFragment类扩展了Fragment类并在其中具有ListView.这是ModulesListFragment如何管理此列表的示例:

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);
}

这是扩展BaseAdapter类的ActionListAdapter重写getView方法的方式:

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;
}

该应用程序最初是针对现在属于片段的五个类中的每一个使用一个活动构建的,现在为了使用制表符,我将其迁移到片段实现中. 该应用程序运行正常,但是当我从一个选项卡切换到另一个选项卡,然后又回到上一个选项卡时,listView就消失了!最后一个没有listView的选项卡可以正常工作,但其他四个选项仅在首次选择时起作用.有没有人知道发生了什么事?我在此处和<在href ="https://stackoverflow.com/questions/17714785/fragment-dont-show-content-of-tabhost-in-second-time">此处,但是它们的实现似乎与我的,因为我现在开始处理片段,所以我无法在解决代码问题的原因和我的问题之间建立联系. 非常感谢您的帮助,我们会及时提供所需的更多信息!在此先感谢甚至阅读此文档的每个人!

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!

推荐答案

更改

@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 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;
}

还有

@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();
}

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);
}

使我们的代码工作.非常感谢@Luksprog!

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

这篇关于包含ListView的选项卡片段在第二次被选择时为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆