引用:http://uuubd.iteye.com/blog/1487343
原文地址:http://blog.csdn.net/hellogv/article/details/6057174
很多客户端软件和浏览器软件都喜欢用Tab分页标签来搭建界面框架。读者也许会马上想到使用TabHost 与 TabActivity的组合,其实最常用的不是它们,而是由GridView与ActivityGroup的组合。每当用户在GridView选中一项,ActivityGroup就把该项对应的Activity的Window作为View添加到ActivityGroup所指定的容器(LinearLayout)中。
效果图:
ImageAdapter是本实例的关键之一,它继承于BaseAdapter,并加入一些自定义的方法。ImageAdapter的源码如下:
SetFocus(int)这个方法是个关键点,即实现选中的效果。例如有ABCD4个Item,其中C被选中了,那么除C以外的Item都被设置为未被选中的样式,而C则设置为选中的样式。
接下来就开始写主Activity,主Activity包含GridView控件,有2点是需要注意一下的。
SetNumColumns():必须要使用setNumColumns来设置列数,因为这个GridView只有一行,即所有的Item都在同一行,Item数量即为列数。
setSelector(new ColorDrawable(Color.TRANSPARENT)):把系统默认选中的背景色透明化,因为我们已经在BaseAdapter中加入了SetFocus()来改变选中的样式。
ActivityGroupActivity.java
main.xml
demo源码:
很多客户端软件和浏览器软件都喜欢用Tab分页标签来搭建界面框架。读者也许会马上想到使用TabHost 与 TabActivity的组合,其实最常用的不是它们,而是由GridView与ActivityGroup的组合。每当用户在GridView选中一项,ActivityGroup就把该项对应的Activity的Window作为View添加到ActivityGroup所指定的容器(LinearLayout)中。
效果图:
ImageAdapter是本实例的关键之一,它继承于BaseAdapter,并加入一些自定义的方法。ImageAdapter的源码如下:
- package org.hwq.activitygroup.main;
- import android.content.Context;
- import android.graphics.Color;
- 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 ImageView[] img;
- ImageAdapter(Context context,int weight,int height,int[] img){
- this.context = context;
- this.img = new ImageView[img.length];
- for(int i=0;i<img.length;i++){
- this.img[i] = new ImageView(context);
- this.img[i].setLayoutParams(new GridView.LayoutParams(weight,height));
- this.img[i].setAdjustViewBounds(false);
- this.img[i].setPadding(2, 2, 2, 2);
- this.img[i].setImageResource(img[i]);
- }
- }
- public void focus(int id){
- for(int i=0;i<img.length;i++){
- if(i == id){
- img[i].setBackgroundColor(Color.YELLOW);
- }else{
- img[i].setBackgroundColor(Color.TRANSPARENT);
- }
- }
- }
- public int getCount() {
- return img.length;
- }
- public Object getItem(int position) {
- return position;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView imageview;
- if(convertView == null){
- imageview = img[position];
- }else{
- imageview = (ImageView) convertView;
- }
- return imageview;
- }
- }
SetFocus(int)这个方法是个关键点,即实现选中的效果。例如有ABCD4个Item,其中C被选中了,那么除C以外的Item都被设置为未被选中的样式,而C则设置为选中的样式。
接下来就开始写主Activity,主Activity包含GridView控件,有2点是需要注意一下的。
SetNumColumns():必须要使用setNumColumns来设置列数,因为这个GridView只有一行,即所有的Item都在同一行,Item数量即为列数。
setSelector(new ColorDrawable(Color.TRANSPARENT)):把系统默认选中的背景色透明化,因为我们已经在BaseAdapter中加入了SetFocus()来改变选中的样式。
ActivityGroupActivity.java
- package org.hwq.activitygroup.main;
- import android.app.ActivityGroup;
- import android.content.Intent;
- import android.graphics.Color;
- import android.graphics.drawable.ColorDrawable;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup.LayoutParams;
- import android.view.Window;
- import android.widget.AdapterView;
- import android.widget.GridView;
- import android.widget.LinearLayout;
- public class ActivityGroupActivity extends ActivityGroup {
- private GridView gridview;
- private LinearLayout layout;
- private int[] img;
- private ImageAdapter adapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- img = new int[]{R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
- gridview = (GridView) findViewById(R.id.gridView1);
- layout = (LinearLayout) findViewById(R.id.linearlayout);
- gridview.setNumColumns(img.length);
- gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
- gridview.setGravity(Gravity.CENTER);
- gridview.setVerticalSpacing(0);
- int weight = getWindowManager().getDefaultDisplay().getWidth()/img.length;
- adapter = new ImageAdapter(this,weight,48,img);
- switchActivity(0);
- gridview.setAdapter(adapter);
- gridview.setOnItemClickListener(new GridviewItemListener());
- }
- class GridviewItemListener implements GridView.OnItemClickListener{
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- switchActivity(position);
- }
- }
- public void switchActivity(int id){
- adapter.focus(id);
- layout.removeAllViews();
- Intent intent = null;
- if(id == 0){
- intent = new Intent(this,ActivityA.class);
- }else if(id == 1){
- intent = new Intent(this,ActivityB.class);
- }else if(id == 2){
- intent = new Intent(this,ActivityC.class);
- }else if(id == 3){
- intent = new Intent(this,ActivityD.class);
- }
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- Window group = getLocalActivityManager().startActivity("", intent);
- layout.addView(group.getDecorView(),LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <GridView
- android:id="@+id/gridView1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:numColumns="3" >
- </GridView>
- <LinearLayout
- android:id="@+id/linearlayout"
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- >
- </LinearLayout>
- </LinearLayout>
demo源码: