Android TabActivity的嵌套

在这篇教程我将告诉你如何使用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和其他嵌套的活动。

你可能感兴趣的:(android,ListView,String,活动,null,import)