包含ListView的选项卡片段在第二次选中时为空白

时间:2014-05-04 15:04:19

标签: java android listview android-fragments android-listview

我的应用程序有一个实现TabListener类的主要活动。在其中,我可以在5个标签之间切换。以下是我在主要活动中的管理方式:

@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如何管理此列表的示例:

@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方法的方法:

@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的选项卡可以正常工作,但其他4个选项仅在第一次选中时才有效。有没有人知道可能发生的事情?我找到了一些有类似问题的人herehere,但他们的实施似乎与我的不同,而且因为我现在正在开始处理碎片,所以我没有能力在他们的代码和我的问题中解决问题的关系...... 任何帮助都非常感谢,并将及时提供所需的任何进一步信息!提前感谢所有阅读此内容的人!

1 个答案:

答案 0 :(得分:1)

更改

@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!