Проблемы SetCurrentTab [Android] - PullRequest
       6

Проблемы SetCurrentTab [Android]

0 голосов
/ 18 апреля 2011

Здравствуйте, я получаю текущую вкладку о повороте экрана со следующим кодом:

@Override
public Object onRetainNonConfigurationInstance()
{
    return CurrentTab;
}

CurrentTab установлено ранее (onTabChange) ...

При повороте экрана я записываю полученные данные:

if (getLastNonConfigurationInstance() != null &&                          
    (Integer)getLastNonConfigurationInstance() >= 0 &&   
    (Integer)getLastNonConfigurationInstance() < 4) 
    {
        CurrentTab = (Integer)getLastNonConfigurationInstance();
        Log.d(" - OOOOK", "Stevilo je: " + CurrentTab.toString());
    }
createView();

В createView у меня есть следующий код:

private void createView()
{
        /** TabHost will have Tabs */
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.setOnTabChangedListener(this);

        tabHost.clearAllTabs();

        /** TabSpec used to create a new tab.
         * By using TabSpec only we can able to setContent to the tab.
         * By using TabSpec setIndicator() we can set name to tab. */

        /** tid1 is firstTabSpec Id. Its used to access outside. */

        TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid1"); 
        TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
        TabSpec forthTabSpec = tabHost.newTabSpec("tid1");

        /** TabSpec setIndicator() is used to set name for the tab. */
        /** TabSpec setContent() is used to set content for a particular tab. */
        firstTabSpec.setIndicator("Kanali", getResources().getDrawable(R.drawable.menu_channels)).setContent(new Intent(this,Channels.class));
        secondTabSpec.setIndicator("Trenutno", getResources().getDrawable(R.drawable.menu_current)).setContent(new Intent(this,Currently.class));
        thirdTabSpec.setIndicator("Opomnik", getResources().getDrawable(R.drawable.menu_reminder)).setContent(new Intent(this,Reminders.class));
        forthTabSpec.setIndicator("O programu", getResources().getDrawable(R.drawable.menu_about)).setContent(new Intent(this,About.class));


        /** Add tabSpec to the TabHost to display. */
        tabHost.addTab(firstTabSpec); 
        tabHost.addTab(secondTabSpec);
        tabHost.addTab(thirdTabSpec);
        tabHost.addTab(forthTabSpec);

        /** applying a theme to app */
        tabHost.setBackgroundColor(Color.WHITE);

        LinearLayout linearTabHost = (LinearLayout) tabHost.getChildAt(0);
        TabWidget tw = (TabWidget) linearTabHost.getChildAt(0);

        /* style and mod tabs */
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
            RelativeLayout rllf = (RelativeLayout) tw.getChildAt(i);
            TextView lf = (TextView) rllf.getChildAt(1);
            lf.setTextSize(12);

            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.BLACK);
        }
        tabHost.getTabWidget().getChildAt(CurrentTab).setBackgroundColor(Color.parseColor("#666666"));
        tabHost.getTabWidget().setCurrentTab(CurrentTab);


}

В результате я выбрал правильную вкладку, но всегда есть содержимое первой вкладки ... (Выбранная вкладка в порядке, показанный контент не) ...

Что я могу сделать?

1 Ответ

1 голос
/ 18 апреля 2011
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid1"); 
        TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");
        TabSpec forthTabSpec = tabHost.newTabSpec("tid1");

измените вышеперечисленное на что-то уникальное.

инициализируйте это так

Intent intent; 

    intent = new Intent().setClass(this, TabHome.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("alaune").setIndicator("A la Une",null)
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, TabHome.class);

    spec = tabHost.newTabSpec("photos").setIndicator("Photos",null)
                  .setContent(intent);

    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabGallery.class);
    spec = tabHost.newTabSpec("videos").setIndicator("Videos",null)
                  .setContent(intent);

    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabVideos.class);

    spec = tabHost.newTabSpec("journal").setIndicator("Journal",null)
                  .setContent(intent);    
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, TabJournal.class);
    spec = tabHost.newTabSpec("direct").setIndicator("Direct",null)
                  .setContent(intent);  

    tabHost.addTab(spec);  
    tabHost.setCurrentTab(0);
...