在android程序中很多客户端软件和浏览器软件都喜欢用tab分页标签来搭建界面框架。读者也许会马上想到使用tabhost 与 tabactivity的组合,其实最常用的不是它们,而是由gridview与activitygroup的组合。每当用户在gridview选中一项,activitygroup就把该项对应的activity的window作为view添加到activitygroup所指定的容器(linearlayout)中。
先来贴出本例运行的效果图如下:
imageadapter是本实例的关键之一,它继承于baseadapter,并加入一些自定义的方法。imageadapter的源码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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点是需要注意一下的。
1.setnumcolumns():必须要使用setnumcolumns来设置列数,因为这个gridview只有一行,即所有的item都在同一行,item数量即为列数。
2.setselector(new colordrawable(color.transparent)):把系统默认选中的背景色透明化,因为我们已经在baseadapter中加入了setfocus()来改变选中的样式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
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); } } |