Android TabActivity的嵌套

來源:互聯網
上載者:User

在這篇教程我將告訴你如何使用TabActivity ActivityGroup一起為了內嵌套的活動。該方法可以有幾個嵌套的活動沒有“失去”選項您標籤導航。
這個想法是用一個LocalActivityManager開始了嵌套的活動。然後替換的看法和觀點ActivityGroup不同的活動。
現在,這聽起來或許複雜,但它是非常簡單的。

首先,TabActivity類。

view plaincopy to clipboardprint?
import android.content.Intent;  
import android.os.Bundle;  
import android.widget.TabHost;  
 
public class TabActivity extends android.app.TabActivity{  
 
    public TabHost tabHost;  
 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.tab_activity_layout);  
 
            // Get the tabHost  
        this.tabHost = getTabHost();  
 
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab  
        Intent intent;  // Reusable Intent for each tab  
 
        // Create an Intent to launch the first Activity for the tab (to be reused)  
        intent = new Intent().setClass(this, FirstGroup.class);  
 
        // Initialize a TabSpec for the first tab and add it to the TabHost  
        spec = tabHost.newTabSpec("FirstGroup").setIndicator("FirstGroup",  
                getResources().getDrawable  
                        (null)) // Replace null with R.drawable.your_icon to set tab icon  
                        .setContent(intent);  
        tabHost.addTab(spec);  
 
            // Create an Intent to launch an Activity for the tab (to be reused)  
        intent = new Intent().setClass(this, SecondActivityGroup.class);  
 
        // Initialize a TabSpec for the second tab and add it to the TabHost  
        spec = tabHost.newTabSpec("SecondGroup").setIndicator("SecondGroup",  
                getResources().getDrawable  
                        (null)) // Replace null with R.drawable.your_icon to set tab icon  
                        .setContent(intent);  
        tabHost.addTab(spec);  
 
        tabHost.setCurrentTab(0);  
    } 
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class TabActivity extends android.app.TabActivity{

 public TabHost tabHost;

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.tab_activity_layout);

            // Get the tabHost
     this.tabHost = getTabHost();

     TabHost.TabSpec spec;  // Resusable TabSpec for each tab
     Intent intent;  // Reusable Intent for each tab

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

     // Initialize a TabSpec for the first tab and add it to the TabHost
     spec = tabHost.newTabSpec("FirstGroup").setIndicator("FirstGroup",
       getResources().getDrawable
         (null)) // Replace null with R.drawable.your_icon to set tab icon
         .setContent(intent);
     tabHost.addTab(spec);

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

     // Initialize a TabSpec for the second tab and add it to the TabHost
     spec = tabHost.newTabSpec("SecondGroup").setIndicator("SecondGroup",
       getResources().getDrawable
         (null)) // Replace null with R.drawable.your_icon to set tab icon
         .setContent(intent);
     tabHost.addTab(spec);

     tabHost.setCurrentTab(0);
 }

好的,那麼這類的活動是所有的標籤。在這個例子中,我剛才建立兩個標籤:“FirstGroup”和“SecondGroup”。接下來我們想做的就是ActivityGroups落實。
我只會讓他們其中之一,因為這個想法是相同的,他們倆:那麼讓我們做FirstGroup.java類:

view plaincopy to clipboardprint?
 
import java.util.ArrayList;  
import android.app.ActivityGroup;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.MenuInflater;  
import android.view.MenuItem;  
import android.view.View;  
 
public class FirstGroup extends ActivityGroup {  
 
        // Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view  
    public static FirstGroup group;  
 
        // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.  
    private ArrayList<VIEW> history;  
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          this.history = new ArrayList<VIEW>();  
          group = this;  
 
              // Start the root activity withing the group and get its view  
          View view = getLocalActivityManager().startActivity("CitiesActivity", new 
                                            Intent(this,CitiesActivity.class)  
                                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
                                            .getDecorView();  
 
              // Replace the view of this ActivityGroup  
          replaceView(view);  
 
       }  
 
    public void replaceView(View v) {  
                // Adds the old one to history  
        history.add(v);  
                // Changes this Groups View to the new View.  
        setContentView(v);  
    }  
 
    public void back() {  
        if(history.size() > 0) {  
            history.remove(history.size()-1);  
            setContentView(history.get(history.size()-1));  
        }else {  
            finish();  
        }  
    }  
 
   @Override 
    public void onBackPressed() {  
        FirstGroup.group.back();  
        return;  
    }  
 
}  
</VIEW></VIEW> 
import java.util.ArrayList;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

public class FirstGroup extends ActivityGroup {

        // Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
 public static FirstGroup group;

        // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
 private ArrayList history;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.history = new ArrayList();
       group = this;

              // Start the root activity withing the group and get its view
       View view = getLocalActivityManager().startActivity("CitiesActivity", new
                   Intent(this,CitiesActivity.class)
                   .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                                      .getDecorView();

              // Replace the view of this ActivityGroup
       replaceView(view);

    }

 public void replaceView(View v) {
                // Adds the old one to history
  history.add(v);
                // Changes this Groups View to the new View.
  setContentView(v);
 }

 public void back() {
  if(history.size() > 0) {
   history.remove(history.size()-1);
   setContentView(history.get(history.size()-1));
  }else {
   finish();
  }
 }

   @Override
    public void onBackPressed() {
     FirstGroup.group.back();
        return;
    }

}

.這個很重要的一部分,上面的代碼是保持這個小組在一個靜態變數,你可以訪問從所有的嵌套的活動。這將讓所有的嵌套的替代ActivityGroup活動。
如果這樣做,你就不會松Tab移動時的不同導航槽內嵌的活動我現在就告訴你怎麼看起來就會像活動的。

view plaincopy to clipboardprint?
import android.app.ListActivity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.ArrayAdapter;  
import android.widget.ListAdapter;  
import android.widget.ListView;  
 
public class CitiesActivity extends ListActivity{  
 
       // Data to put in the ListAdapter  
       private String[] cities = new String[] {"Bergen", "New York", "Paris"}  
        private ListAdapter adapter;  
 
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.cities_activity_layout);  
        fillData();  
    }  
 
    // Fill the list with some data, this can be anything really  
    public void fillData() {  
        ArrayAdapter citiesAdapter = new ArrayAdapter(this, R.layout.city_row, cities);  
        setListAdapter(citiesAdapter);  
    }  
 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) {  
        super.onListItemClick(l, v, position, id);  
 
        Intent i = new Intent(this, ShowCity.class);  
        String city_name = (String) getListAdapter().getItem(position);  
        i.putExtra("city_name", city_name);  
 
        // Create the view using FirstGroup's LocalActivityManager  
        View view = FirstGroup.group.getLocalActivityManager()  
        .startActivity("show_city", i  
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
        .getDecorView();  
 
        // Again, replace the view  
        FirstGroup.group.replaceView(view);  
    }  

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class CitiesActivity extends ListActivity{

       // Data to put in the ListAdapter
       private String[] cities = new String[] {"Bergen", "New York", "Paris"}
     private ListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cities_activity_layout);
        fillData();
    }

    // Fill the list with some data, this can be anything really
    public void fillData() {
     ArrayAdapter citiesAdapter = new ArrayAdapter(this, R.layout.city_row, cities);
        setListAdapter(citiesAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
     super.onListItemClick(l, v, position, id);

        Intent i = new Intent(this, ShowCity.class);
        String city_name = (String) getListAdapter().getItem(position);
        i.putExtra("city_name", city_name);

        // Create the view using FirstGroup's LocalActivityManager
        View view = FirstGroup.group.getLocalActivityManager()
        .startActivity("show_city", i
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
        .getDecorView();

        // Again, replace the view
        FirstGroup.group.replaceView(view);
    }
}

上面的代碼僅僅顯示列表的一些城市,然後,如果你點擊任何本列表條目將取代了FirstGroup的觀點和角度ShowCity.java創造的活動。你可以做同樣的事情,ShowCity和其他嵌套的活動。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.