ActivityGroup + GridView 实现Tab分页标签

很多客户端软件和浏览器软件都喜欢用Tab分页标签来搭建界面框架。读者也许会马上想到使用TabHost 与 TabActivity的组合,其实最常用的不是它们,而是由GridView与ActivityGroup的组合。每当用户在GridView选中一项,ActivityGroup就把该项对应的Activity的Window作为View添加到ActivityGroup所指定的容器(LinearLayout)中。

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

ActivityGroup + GridView 实现Tab分页标签_第1张图片

ImageAdapter是本实例的关键之一,它继承于BaseAdapter,并加入一些自定义的方法。ImageAdapter的源码如下:

[java] view plain copy print ?
  1. package com.ActivityGroupDemo;
  2. import android.content.Context;
  3. import android.graphics.drawable.ColorDrawable;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseAdapter;
  7. import android.widget.GridView;
  8. import android.widget.ImageView;
  9. /**
  10. *
  11. * @author GV
  12. *
  13. */
  14. public class ImageAdapter extends BaseAdapter {
  15. private Context mContext;
  16. private ImageView[] imgItems;
  17. private int selResId;
  18. public ImageAdapter(Context c,int[] picIds,int width,int height,int selResId) {
  19. mContext = c;
  20. this.selResId=selResId;
  21. imgItems=new ImageView[picIds.length];
  22. for(int i=0;i<picIds.length;i++)
  23. {
  24. imgItems[i] = new ImageView(mContext);
  25. imgItems[i].setLayoutParams(new GridView.LayoutParams(width, height));//设置ImageView宽高
  26. imgItems[i].setAdjustViewBounds(false);
  27. //imgItems[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
  28. imgItems[i].setPadding(2, 2, 2, 2);
  29. imgItems[i].setImageResource(picIds[i]);
  30. }
  31. }
  32. public int getCount() {
  33. return imgItems.length;
  34. }
  35. public Object getItem(int position) {
  36. return position;
  37. }
  38. public long getItemId(int position) {
  39. return position;
  40. }
  41. /**
  42. * 设置选中的效果
  43. */
  44. public void SetFocus(int index)
  45. {
  46. for(int i=0;i<imgItems.length;i++)
  47. {
  48. if(i!=index)
  49. {
  50. imgItems[i].setBackgroundResource(0);//恢复未选中的样式
  51. }
  52. }
  53. imgItems[index].setBackgroundResource(selResId);//设置选中的样式
  54. }
  55. public View getView(int position, View convertView, ViewGroup parent) {
  56. ImageView imageView;
  57. if (convertView == null) {
  58. imageView=imgItems[position];
  59. } else {
  60. imageView = (ImageView) convertView;
  61. }
  62. return imageView;
  63. }
  64. }
package com.ActivityGroupDemo;import android.content.Context;import android.graphics.drawable.ColorDrawable;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;/** * * @author GV * */public class ImageAdapter extends BaseAdapter { private Context mContext; private ImageView[] imgItems; private int selResId; public ImageAdapter(Context c,int[] picIds,int width,int height,int selResId) { mContext = c; this.selResId=selResId; imgItems=new ImageView[picIds.length]; for(int i=0;i<picIds.length;i++) { imgItems[i] = new ImageView(mContext); imgItems[i].setLayoutParams(new GridView.LayoutParams(width, height));//设置ImageView宽高 imgItems[i].setAdjustViewBounds(false); //imgItems[i].setScaleType(ImageView.ScaleType.CENTER_CROP); imgItems[i].setPadding(2, 2, 2, 2); imgItems[i].setImageResource(picIds[i]); } } public int getCount() { return imgItems.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } /** * 设置选中的效果 */ public void SetFocus(int index) { for(int i=0;i<imgItems.length;i++) { if(i!=index) { imgItems[i].setBackgroundResource(0);//恢复未选中的样式 } } imgItems[index].setBackgroundResource(selResId);//设置选中的样式 } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView=imgItems[position]; } else { imageView = (ImageView) convertView; } return imageView; } }

SetFocus(int)这个方法是个关键点,即实现选中的效果。例如有ABCD4个Item,其中C被选中了,那么除C以外的Item都被设置为未被选中的样式,而C则设置为选中的样式。

接下来就开始写主Activity,主Activity包含GridView控件,名为gvTopBar,有2点是需要注意一下的。

  • SetNumColumns():必须要使用setNumColumns来设置列数,因为这个GridView只有一行,即所有的Item都在同一行,Item数量即为列数。
  • setSelector(new ColorDrawable(Color.TRANSPARENT)):把系统默认选中的背景色透明化,因为我们已经在BaseAdapter中加入了SetFocus()来改变选中的样式。
[java] view plain copy print ?
  1. package com.ActivityGroupDemo;
  2. import android.app.Activity;
  3. import android.app.ActivityGroup;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.graphics.Color;
  9. import android.graphics.drawable.ColorDrawable;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.Gravity;
  13. import android.view.View;
  14. import android.view.Window;
  15. import android.view.ViewGroup.LayoutParams;
  16. import android.widget.AdapterView;
  17. import android.widget.GridView;
  18. import android.widget.LinearLayout;
  19. import android.widget.Toast;
  20. import android.widget.AdapterView.OnItemClickListener;
  21. /**
  22. *
  23. * @author GV
  24. *
  25. */
  26. public class ActivityGroupDemo extends ActivityGroup {
  27. private GridView gvTopBar;
  28. private ImageAdapter topImgAdapter;
  29. public LinearLayout container;// 装载sub Activity的容器
  30. /** 顶部按钮图片 **/
  31. int[] topbar_image_array = { R.drawable.topbar_home,
  32. R.drawable.topbar_user, R.drawable.topbar_shoppingcart,
  33. R.drawable.topbar_note };
  34. @Override
  35. public void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.main);
  38. gvTopBar = (GridView) this.findViewById(R.id.gvTopBar);
  39. gvTopBar.setNumColumns(topbar_image_array.length);// 设置每行列数
  40. gvTopBar.setSelector(new ColorDrawable(Color.TRANSPARENT));// 选中的时候为透明色
  41. gvTopBar.setGravity(Gravity.CENTER);// 位置居中
  42. gvTopBar.setVerticalSpacing(0);// 垂直间隔
  43. int width = this.getWindowManager().getDefaultDisplay().getWidth()
  44. / topbar_image_array.length;
  45. topImgAdapter = new ImageAdapter(this, topbar_image_array, width, 48,
  46. R.drawable.topbar_itemselector);
  47. gvTopBar.setAdapter(topImgAdapter);// 设置菜单Adapter
  48. gvTopBar.setOnItemClickListener(new ItemClickEvent());// 项目点击事件
  49. container = (LinearLayout) findViewById(R.id.Container);
  50. SwitchActivity(0);//默认打开第0页
  51. }
  52. class ItemClickEvent implements OnItemClickListener {
  53. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  54. long arg3) {
  55. SwitchActivity(arg2);
  56. }
  57. }
  58. /**
  59. * 根据ID打开指定的Activity
  60. * @param id GridView选中项的序号
  61. */
  62. void SwitchActivity(int id)
  63. {
  64. topImgAdapter.SetFocus(id);//选中项获得高亮
  65. container.removeAllViews();//必须先清除容器中所有的View
  66. Intent intent =null;
  67. if (id == 0 || id == 2) {
  68. intent = new Intent(ActivityGroupDemo.this, ActivityA.class);
  69. } else if (id == 1 || id == 3) {
  70. intent = new Intent(ActivityGroupDemo.this, ActivityB.class);
  71. }
  72. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  73. //Activity 转为 View
  74. Window subActivity = getLocalActivityManager().startActivity(
  75. "subActivity", intent);
  76. //容器添加View
  77. container.addView(subActivity.getDecorView(),
  78. LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
  79. }
  80. }
package com.ActivityGroupDemo;import android.app.Activity;import android.app.ActivityGroup;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;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.Toast;import android.widget.AdapterView.OnItemClickListener;/** * * @author GV * */public class ActivityGroupDemo extends ActivityGroup { private GridView gvTopBar; private ImageAdapter topImgAdapter; public LinearLayout container;// 装载sub Activity的容器 /** 顶部按钮图片 **/ int[] topbar_image_array = { R.drawable.topbar_home, R.drawable.topbar_user, R.drawable.topbar_shoppingcart, R.drawable.topbar_note }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gvTopBar = (GridView) this.findViewById(R.id.gvTopBar); gvTopBar.setNumColumns(topbar_image_array.length);// 设置每行列数 gvTopBar.setSelector(new ColorDrawable(Color.TRANSPARENT));// 选中的时候为透明色 gvTopBar.setGravity(Gravity.CENTER);// 位置居中 gvTopBar.setVerticalSpacing(0);// 垂直间隔 int width = this.getWindowManager().getDefaultDisplay().getWidth() / topbar_image_array.length; topImgAdapter = new ImageAdapter(this, topbar_image_array, width, 48, R.drawable.topbar_itemselector); gvTopBar.setAdapter(topImgAdapter);// 设置菜单Adapter gvTopBar.setOnItemClickListener(new ItemClickEvent());// 项目点击事件 container = (LinearLayout) findViewById(R.id.Container); SwitchActivity(0);//默认打开第0页 } class ItemClickEvent implements OnItemClickListener { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { SwitchActivity(arg2); } } /** * 根据ID打开指定的Activity * @param id GridView选中项的序号 */ void SwitchActivity(int id) { topImgAdapter.SetFocus(id);//选中项获得高亮 container.removeAllViews();//必须先清除容器中所有的View Intent intent =null; if (id == 0 || id == 2) { intent = new Intent(ActivityGroupDemo.this, ActivityA.class); } else if (id == 1 || id == 3) { intent = new Intent(ActivityGroupDemo.this, ActivityB.class); } intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //Activity 转为 View Window subActivity = getLocalActivityManager().startActivity( "subActivity", intent); //容器添加View container.addView(subActivity.getDecorView(), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); }}

主Activity的布局XML文件源码如下:

[java] view plain copy print ?
  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. <RelativeLayout android:layout_height="fill_parent"
  6. android:layout_width="fill_parent">
  7. <GridView android:layout_height="wrap_content" android:id="@+id/gvTopBar"
  8. android:layout_alignParentTop="true" android:layout_width="fill_parent"
  9. android:fadingEdgeLength="5dip" android:fadingEdge="vertical">
  10. </GridView>
  11. <LinearLayout android:id="@+id/Container"
  12. android:layout_below="@+id/gvTopBar" android:layout_width="fill_parent"
  13. android:layout_height="fill_parent">
  14. </LinearLayout>
  15. </RelativeLayout>
  16. </LinearLayout>

你可能感兴趣的:(android,object,image,layout,Class,import)