开发者

Change tab using onClickItem in ListActivity

Can someone please tell me how to change tab by clicking on element INSIDE the tab? I already tried it with global data. The code looks like this:

public class Tabs extends TabActivity {

int tabNumber = 0;
private TabHost tabHost;
int returnedTabNumber = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Resources res = getResources(); // Resource object to get Drawables
    tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, Tribocracy.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("map").setIndicator("Map",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, Areas.class);
    spec = tabHost.newTabSpec("areas").setIndicator("Areas",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, Settings.class);
    spec = tabHost.newTabSpec("settings").setIndicator("Settings",
                      res.getDrawable(R.drawable.ic_tab_albums))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(tabNumber);


}

protected void onResume() {
    super.onResume();


            GlobalData globalData = ((GlobalData)getApplicationContext());
            returnedTabNumber = globalData.getTabNumber();
            tabHost.setCurrentTab(returnedTabNumber);   


}

}

The global adapter looks like this:

public class GlobalData extends Applic开发者_Go百科ation {
//----------------------------------------------------
      private int Point1;   //define the vars here
      private int Point2;   //define the vars here
      private int Point3;   //define the vars here
      private int Point4;   //define the vars here
      private int Point5;   //define the vars here
      private int Point6;   //define the vars here
      private int tabNumber;

      public int getTabNumber() //getter of the value
      {     
        return tabNumber;
      }

      public int setTabNumber(int number)     //setter of the value
      {
        tabNumber = number;
        return tabNumber;
      }

}

Now when I'm trying to change tab in my ListActivity tab by clicking on one of the items it doesn't do anything and stays on the ListActivity tab. Perhaps I shouldn't use onResume() here. Basically I want to go to first tab when I click on one of the items in the list. Please help!


One way to do this is to use Intents. And there are many ways to use Intents for this. This works especially well for some cases, for example where the activity is displaying some content from a content provider. I'll describe one possible method below:

  1. For the Activity that hosts the tabs, give it an intent filter if it doesn't already have one. For example, if it's an 'item detail'-style Activity, maybe its intent filter can match for ACTION_VIEW on a certain content type like vnd.android.cursor.item/vnd.somecontent.
  2. Have the Activity take an extra such as focus_tab, and in the Activity's onCreate, after setting up the tabs, set the active tab to the one specified by this focus_tab extra.
  3. Override onNewIntent, and in that method, look for that same focus_tab extra and set the active to the one specified by that extra.
  4. Now, in your button code (i.e. a button inside one of your tab content activities), create an Intent that will start your host Activity, and give it a focus_tab extra corresponding to the tab you want to switch to. Add the FLAG_ACTIVITY_SINGLE_TOP Intent flag so you don't create a new instance of the host Activity. And then, call startActivity on that Intent you just created.

Note: I haven't tried this myself, but it should work in theory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜