Android入门第十五篇之ActivityGroup实现Tab分页标签

很多客户端软件和浏览器软件都喜欢用Tab分页标签来管理内容,除了可以用TabHost控件,还可以用ImageButton + ActivityGroup实现Tab分页标签。使用ImageButton + ActivityGroup实现Tab分页标签,主要是把一个Sub Activity(子Activity)的Window作为View添加到ActivityGroup所指定的容器中,本文使用LinearLayout 作为容器装载Sub Activity。

接下来贴出本例运行的效果图

以下是切换时Sub Activity的生存周期的状态变化:

从subActivity1切换到subActivity2的时候,会彻底释放subActivity1的资源。

主Activity的main.xml的源码如下:

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
  3.     android:orientation = "vertical"   android:layout_width = "fill_parent"   
  4.     android:layout_height = "fill_parent" >   
  5.     < LinearLayout   android:id = "@+id/LinearLayout01"   
  6.         android:layout_height = "wrap_content"   android:layout_width = "fill_parent" >   
  7.         < ImageButton   android:layout_width = "wrap_content"   
  8.             android:layout_height = "wrap_content"   android:id = "@+id/ibtnTab1"   
  9.             android:background = "@drawable/png1298" > </ ImageButton >   
  10.         < ImageButton   android:layout_width = "wrap_content"   
  11.             android:layout_height = "wrap_content"   android:id = "@+id/ibtnTab2"   
  12.             android:background = "@drawable/png1292" > </ ImageButton >   
  13.     </ LinearLayout >   
  14.     < LinearLayout   android:id = "@+id/LinearLayout02"   
  15.         android:layout_width = "fill_parent"   android:layout_height = "fill_parent" > </ LinearLayout >   
  16. </ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="fill_parent"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ibtnTab1" android:background="@drawable/png1298"></ImageButton> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ibtnTab2" android:background="@drawable/png1292"></ImageButton> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="fill_parent" android:layout_height="fill_parent"></LinearLayout> </LinearLayout>

Sub Activity的XML源码(listview.xml)如下:

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < LinearLayout   android:id = "@+id/LinearLayout01"   
  3.     xmlns:android = "http://schemas.android.com/apk/res/android"   
  4.     android:layout_width = "fill_parent"   android:layout_height = "fill_parent" >   
  5.     < ListView   android:id = "@+id/MyListView"   android:layout_width = "fill_parent"   
  6.         android:layout_height = "fill_parent" >   
  7.     </ ListView >   
  8. </ LinearLayout >     
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/MyListView" android:layout_width="fill_parent" android:layout_height="fill_parent"> </ListView> </LinearLayout>

testActivityGroup.java源码如下:

  1. package  com.testActivityGroup;  
  2.   
  3. import  android.app.ActivityGroup;  
  4. import  android.content.Intent;  
  5. import  android.os.Bundle;  
  6. import  android.view.View;  
  7. import  android.view.Window;  
  8. import  android.widget.ImageButton;  
  9. import  android.widget.LinearLayout;  
  10. import  android.widget.ListView;  
  11.   
  12. public   class  testActivityGroup  extends  ActivityGroup {  
  13.     /** Called when the activity is first created. */   
  14.     LinearLayout container;//装载sub Activity的容器   
  15.     ImageButton ibtnTab1,ibtnTab2;  
  16.     @Override   
  17.     public   void  onCreate(Bundle savedInstanceState) {  
  18.         super .onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.           
  21.         container = (LinearLayout) findViewById(R.id.LinearLayout02);  
  22.         ibtnTab1=(ImageButton)this .findViewById(R.id.ibtnTab1);  
  23.         ibtnTab1.setOnClickListener(new  ClickEvent());  
  24.         ibtnTab2=(ImageButton)this .findViewById(R.id.ibtnTab2);  
  25.         ibtnTab2.setOnClickListener(new  ClickEvent());  
  26.     }  
  27.       
  28.     class  ClickEvent  implements  View.OnClickListener{  
  29.   
  30.         @Override   
  31.         public   void  onClick(View v) {  
  32.             container.removeAllViews();  
  33.             Intent intent=new  Intent(testActivityGroup. this , subActivity. class );  
  34.             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  35.             String[] str=new  String[ 12 ];  
  36.             if (v==ibtnTab1)  
  37.             {  
  38.                 for ( int  i= 0 ;i<str.length;i++)  
  39.                     str[i]="单选" +String.valueOf(i);  
  40.                 intent.putExtra("Name""subActivity1" );  
  41.                 intent.putExtra("Strings" , str);  
  42.                 intent.putExtra("ChoiceMode" , ListView.CHOICE_MODE_SINGLE); //通过参数设置列表式样   
  43.             }  
  44.             else   if (v==ibtnTab2)  
  45.             {  
  46.                 for ( int  i= 0 ;i<str.length;i++)  
  47.                     str[i]="复选" +String.valueOf(i);  
  48.                 intent.putExtra("Name""subActivity2" );  
  49.                 intent.putExtra("Strings" , str);  
  50.                 intent.putExtra("ChoiceMode" , ListView.CHOICE_MODE_MULTIPLE); //通过参数设置列表式样   
  51.             }  
  52.   
  53.             Window subActivity=getLocalActivityManager().startActivity("subActivity" ,intent);  
  54.             container.addView(subActivity.getDecorView());  
  55.         }  
  56.           
  57.     }  
  58. }  

package com.testActivityGroup; import android.app.ActivityGroup; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.ListView; public class testActivityGroup extends ActivityGroup { /** Called when the activity is first created. */ LinearLayout container;//装载sub Activity的容器 ImageButton ibtnTab1,ibtnTab2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); container = (LinearLayout) findViewById(R.id.LinearLayout02); ibtnTab1=(ImageButton)this.findViewById(R.id.ibtnTab1); ibtnTab1.setOnClickListener(new ClickEvent()); ibtnTab2=(ImageButton)this.findViewById(R.id.ibtnTab2); ibtnTab2.setOnClickListener(new ClickEvent()); } class ClickEvent implements View.OnClickListener{ @Override public void onClick(View v) { container.removeAllViews(); Intent intent=new Intent(testActivityGroup.this, subActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); String[] str=new String[12]; if(v==ibtnTab1) { for(int i=0;i<str.length;i++) str[i]="单选"+String.valueOf(i); intent.putExtra("Name", "subActivity1"); intent.putExtra("Strings", str); intent.putExtra("ChoiceMode", ListView.CHOICE_MODE_SINGLE);//通过参数设置列表式样 } else if(v==ibtnTab2) { for(int i=0;i<str.length;i++) str[i]="复选"+String.valueOf(i); intent.putExtra("Name", "subActivity2"); intent.putExtra("Strings", str); intent.putExtra("ChoiceMode", ListView.CHOICE_MODE_MULTIPLE);//通过参数设置列表式样 } Window subActivity=getLocalActivityManager().startActivity("subActivity",intent); container.addView(subActivity.getDecorView()); } } }

subActivity.java源码如下:

  1. package  com.testActivityGroup;  
  2.   
  3. import  android.app.Activity;  
  4. import  android.os.Bundle;  
  5. import  android.util.Log;  
  6. import  android.widget.ArrayAdapter;  
  7. import  android.widget.ListView;  
  8.   
  9. public   class  subActivity  extends  Activity {  
  10.     String name;  
  11.   
  12.     public   void  onCreate(Bundle savedInstanceState) {  
  13.         super .onCreate(savedInstanceState);  
  14.         setContentView(R.layout.listview);  
  15.   
  16.         // 读取列表内容   
  17.         name = this .getIntent().getStringExtra( "Name" );  
  18.         String[] str = this .getIntent().getStringArrayExtra( "Strings" );  
  19.         int  choiceMode =  this .getIntent().getIntExtra( "ChoiceMode" ,  
  20.                 ListView.CHOICE_MODE_NONE);  
  21.   
  22.         ListView listView = (ListView) findViewById(R.id.MyListView);  
  23.   
  24.         // 设置列表的式样   
  25.         int  itemID = android.R.layout.simple_list_item_1;  
  26.         if  (choiceMode == ListView.CHOICE_MODE_MULTIPLE) // 主Activity要求多选   
  27.             itemID = android.R.layout.simple_list_item_multiple_choice;  
  28.         else   if  (choiceMode == ListView.CHOICE_MODE_SINGLE) // 主Activity要求单选   
  29.             itemID = android.R.layout.simple_list_item_single_choice;  
  30.   
  31.         ArrayAdapter<String> arrayAdapter = new  ArrayAdapter<String>( this ,  
  32.                 itemID, str);  
  33.         listView.setAdapter(arrayAdapter);  
  34.   
  35.         listView.setChoiceMode(choiceMode);  
  36.   
  37.         Log.e(name, "onCreate" ); // 显示当前状态,onCreate与onDestroy对应   
  38.     }  
  39.     @Override   
  40.     public   void  onDestroy() {  
  41.         super .onDestroy();  
  42.         Log.e(name, "onDestroy" ); // 显示当前状态,onCreate与onDestroy对应   
  43.     }  
  44.   
  45.     @Override   
  46.     public   void  onStart() {  
  47.         super .onStart();  
  48.         Log.e(name, "onStart" ); // 显示当前状态,onStart与onStop对应   
  49.     }  
  50.   
  51.     @Override   
  52.     public   void  onStop() {  
  53.         super .onStop();  
  54.         Log.e(name, "onStop" ); // 显示当前状态,onStart与onStop对应   
  55.     }  
  56.   
  57.     @Override   
  58.     public   void  onRestart() {  
  59.         super .onRestart();  
  60.         Log.e(name, "onRestart" );  
  61.     }  
  62.   
  63.     @Override   
  64.     public   void  onResume() {  
  65.         super .onResume();  
  66.         Log.e(name, "onResume" ); // 显示当前状态,onPause与onResume对应   
  67.     }  
  68.   
  69.     @Override   
  70.     public   void  onPause() {  
  71.         super .onResume();  
  72.         Log.e(name, "onPause" ); // 显示当前状态,onPause与onResume对应   
  73.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值