Many client software and browser software in the Android program like to use tab tabs to build the interface frame. Readers may immediately think of using the combination of tabhost and tabactivity, but the most commonly used is not them, but the combination of the GridView and the Activitygroup. Whenever the user selects an item in the GridView, Activitygroup adds the window of the corresponding activity to the container (linearlayout) specified by the Activitygroup as a view.
First, we'll post the results of this example run as follows:
Imageadapter is one of the keys to this example, it inherits from Baseadapter and adds some custom methods. Imageadapter source code is as follows:
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));//Set ImageView width high 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;
/** * Set Selected effect */public void SetFocus (int index) {for (int i=0;i
where SetFocus (int) This method is a key point, that is, to achieve the selected effect. For example, if you have ABCD4 item, where C is selected, the item except C is set to a style that is not selected, and C is set to the selected style.
Then start writing the main activity, which contains the GridView control, called Gvtopbar, 2 points that need to be noted.
1.setnumcolumns (): You must use Setnumcolumns to set the number of columns, because the GridView has only one row, that is, all the item is on the same line and the item quantity is the number of columns.
2. Setselector (New colordrawable (color.transparent)): Make the system default selected background color transparent, because we have added SetFocus () to the Baseadapter to change the selected style.
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;
The public linearlayout container;//the container that loads the sub activity/** the top button picture **/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);/Set the number of rows per row gvtopbar.setselector (new colordrawable (
color.transparent);//Gvtopbar.setgravity (Gravity.center) for transparent color when selected;/position centered gvtopbar.setverticalspacing (0);//Vertical interval
int width = This.getwindowmanager (). Getdefaultdisplay (). GetWidth ()/topbar_image_array.length;
Topimgadapter = new Imageadapter (this, Topbar_image_array, width, r.drawable.topbar_itemselector);
Gvtopbar.setadapter (Topimgadapter)//Set Menu adapter Gvtopbar.setonitemclicklistener (new Itemclickevent ());//Item Click event
container = (linearlayout) Findviewbyid (R.id.container); Switchactivity (0);//Default open NO. 0 page} class Itemclickevent implements Onitemclicklistener {public void Onitemclick (Adaptervie W<?> arg0, View arg1, int arg2, long arg3){switchactivity (ARG2); }/** * Opens the specified activity * Based on ID @param ID The ordinal number of the selected item/void switchactivity (int id) {Topimgadapter.setfocus (ID)
;//Selected to get the highlight container.removeallviews ();//must first clear all view Intent Intent =null in the container;
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 into View Window subactivity = Getlocalactivitymanager (). StartActivity ("subactivity", intent);
The container adds view Container.addview (Subactivity.getdecorview (), layoutparams.fill_parent, layoutparams.fill_parent); }
}