在前面,已经介绍了TabActivity和TabHost的结合实现Tab分页的三种方式。这里,将讲到采用ActivityGroup和GridView的结合,实现Tab分页。其实,这和TabActivity和TabHost的结合差不多。只不过,是自己把Tab的头部(选项栏)和页面视图分开实现。Tab头部采用GridView来实现,子页面采用LinearLayout容器来实现。然后把每个子Activity作为一个View视图,装载到LinearLayout容器里面。你会发现LinearLayout不仅仅是布局容器,它还有许多妙用。
实现讲解:
1、主类继承ActivityGroup
public class GridViewTabPage extends ActivityGroup
2、 获取每个子页面的Activity视图
Intent intent = new Intent(GridViewTabPage.this, Page1.class);
subPageView = getLocalActivityManager().startActivity(
"subPageView" + i, intent);
3、加载到容器
pageContainer.addView(subPageView .getDecorView(),
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
1、布局XML文件:main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- android:id="@+id/gridViewTab"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- >
- <GridView
- android:id="@+id/gv_tabPage"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:numColumns="3"
- >
- </GridView>
- <LinearLayout
- android:id="@+id/pageContainer"
- android:layout_below="@+id/gvTopBar"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- </LinearLayout>
-
- </LinearLayout>
2、代码文件:
图片适配器:ImageAdapter.java
- package com.myandroid.test;
-
- import android.content.Context;
- import android.graphics.Color;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
-
- public class ImageAdapter extends BaseAdapter {
- private Context context;
- private int width;
- private int height;
- private ImageView[] imageViews;
- private Integer[] imgeIDs;
-
- public ImageAdapter(Context context, Integer[] imageIDs, int width, int height) {
- this.context = context;
- this.imgeIDs = imageIDs;
- this.width = width;
- this.height = height;
- imageViews = new ImageView[imageIDs.length];
- for(int i = 0; i < imageViews.length; i++) {
- imageViews[i] = new ImageView(context);
- }
- }
-
- public int getCount() {
-
- return imgeIDs.length;
- }
-
- public Object getItem(int position) {
-
- return position;
- }
-
- public long getItemId(int position) {
-
- return position;
- }
-
-
-
-
-
- public void setFocus(int selectedID) {
- Log.e("Seqence", "setFocus");
- for(int i = 0; i < imageViews.length; i++) {
- imageViews[i].setBackgroundColor(Color.BLACK);
- }
- imageViews[selectedID].setBackgroundColor(Color.YELLOW);
- }
-
-
-
-
- public View getView(int position, View convertView, ViewGroup parent) {
-
- Log.e("Seqence", "getView");
- imageViews[position].setImageResource(imgeIDs[position]);
- imageViews[position].setLayoutParams(new GridView.LayoutParams(width, height));
- imageViews[position].setBackgroundColor(android.R.drawable.picture_frame);
-
- return imageViews[position];
- }
-
- }
主文件:GridViewTabPage.java
- package com.myandroid.test;
-
-
- import android.app.ActivityGroup;
- import android.content.Intent;
- import android.graphics.Color;
- import android.graphics.drawable.ColorDrawable;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.View;
- import android.view.Window;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.AdapterView;
- import android.widget.GridView;
- import android.widget.LinearLayout;
- import android.widget.AdapterView.OnItemClickListener;
-
- public class GridViewTabPage extends ActivityGroup {
-
- private GridView gv_tabPage;
- private ImageAdapter imageAdapter;
- public LinearLayout pageContainer;
- private Intent[] intents;
- private Window[] subPageView;
- private Integer[] tabImages = { R.drawable.a1,
- R.drawable.a2, R.drawable.a3,};
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Log.e("Sequence", "start");
- gv_tabPage = (GridView) findViewById(R.id.gv_tabPage);
- gv_tabPage.setNumColumns(tabImages.length);
- gv_tabPage.setSelector(new ColorDrawable(Color.TRANSPARENT));
- gv_tabPage.setGravity(Gravity.CENTER);
- gv_tabPage.setVerticalSpacing(0);
- int width = this.getWindowManager().getDefaultDisplay().getWidth()
- / tabImages.length;
- imageAdapter = new ImageAdapter(this, tabImages, width, 48);
- gv_tabPage.setAdapter(imageAdapter);
- gv_tabPage.setOnItemClickListener(new ItemClickEvent());
- pageContainer = (LinearLayout) findViewById(R.id.pageContainer);
- SwitchPage(0);
- Log.e("Sequence", "end");
- }
-
- class ItemClickEvent implements OnItemClickListener {
-
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- SwitchPage(arg2);
- }
- }
-
-
-
-
-
-
-
-
- private Window getPageView(int pageID) {
- if(intents == null) {
- intents = new Intent[3];
- subPageView = new Window[3];
- intents[0] = new Intent(GridViewTabPage.this, Page1.class);
- intents[1] = new Intent(GridViewTabPage.this, Page2.class);
- intents[2] = new Intent(GridViewTabPage.this, Page3.class);
- for(int i = 0; i < 3; i++) {
- subPageView[i] = getLocalActivityManager().startActivity(
- "subPageView" + i, intents[i]);
- }
- Log.e("New", "new");
- }
-
- return subPageView[pageID];
- }
-
-
-
-
-
- private void SwitchPage(int id)
- {
- pageContainer.removeAllViews();
- imageAdapter.setFocus(id);
- Window pageView = null;
- switch(id){
- case 0:
- pageView = getPageView(0);
- break;
- case 1:
- pageView = getPageView(1);
- break;
- case 2:
- pageView = getPageView(2);
- break;
- default:
- break;
- }
-
- pageContainer.addView(pageView.getDecorView(),
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
- }
- }
3、最后是在AndroidManifest.xml里添加Activity注册信息
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.myandroid.test"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- ................
-
- <activity android:name="Page1"></activity>
- <activity android:name="Page2"></activity>
- <activity android:name="Page3"></activity>
- </application>
- </manifest>
同样的,对于Tab选项过多,可以采用Grally+ActivityGroup结合的实现方式。具体实现方法,和ActivityGroup和GridView的结合差不多,读者可以自己实现。