實現Tabhost每個tab中放入多個Activity!

來源:互聯網
上載者:User

最近做完了一個用戶端項目

外面是個tabhost(用於底部tab欄)每個tab中有一個Activity,這個很普遍。

但有時需要每個tab中有多個頁面,也就是說需要 在一個tab中跳轉不同的Activity。

當時,我只是簡單的在一個Activity中使用了動態布局 (設定layout是否顯示)

雖然效果還不錯,但實際上依然是同一個Activity,這樣的缺點是代碼結構會相對複雜,不易維護

今天在網上看到了一篇文章,原文是http://united-coders.com網站的http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity

特意留下來 供大家和自己以後使用:

 

 

Use Android ActivityGroup within TabHost to show different Activity

 

 

For that reason you need a ActivityGroup within the Tab where you want to change the
Activity.
An ActivityGroup is: A screen that contains and runs multiple embedded activities.

Let's look at this at a real life example. An Android app that shows your podcasts. In the first Activity you get to see all podcasts by subscription. If you touch the subscription, you see the single podcasts you've downloaded
for that subscription.

The Tabhost contains three tabs, one for the MediaPlayer, one for the archive and one for available downloads.

  1.         TabHost tabHost = getTabHost();
  2.  
  3.         tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Player").setContent(
  4.                 new Intent(this,
    PlayerActivity.class)));
  5.         tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Archive").setContent(
  6.                 new Intent(this,
    ArchiveGroup.class)));
  7.         tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Updates").setContent(
  8.                 new Intent(this,
    DownloadList.class)));

 

The ArchiveGroup takes care which Activity is shown in the second tab. With setContentView you can bring the View to the front.

  1.         View view = getLocalActivityManager().startActivity("ArchiveActivity",
  2.                 new Intent(this,
    ArchiveActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
  3.        
  4.         setContentView(view);

 

Now all you need to do is to bring another View to the front after an action is triggered. In this case, after a ListItem is clicked.

  1.         String album = (String) getListView().getItemAtPosition(position);
  2.         Intent intent = new Intent(getApplicationContext(),
    ArchiveAlbums.class);
  3.         intent.putExtra("album", album);
  4.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  5.        
  6.         View view = ArchiveGroup.group.getLocalActivityManager().startActivity("ShowPodcasts",
    intent).getDecorView();
  7.        
  8.         ArchiveGroup.group.setContentView(view);


You need to tell the ActivityGroup which view is to be on top and set the appropriate
Flag, so that the View will be on top.

 

Of course now you have to keep track which activity is in front, how they behave and what happens if the back button is pressed. But that gives you room for customizing the behavior.

The full code can be found on github in the GpodRoid project.

Another good tutorials are by H. Larsen and Eric
Harlow.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.