惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 生如夏花之灿烂

本人讲课时录制的Android应用开发技术教学视频 jsoup使用样式class抓取数据时空格的处理 我的第一款anroid软件作品--短信精灵1.0 (原创)Android入门教程(三十六)------实现手机联系人的全选 Android入门教程(三十五)------在Android上使用ZXing识别条形码/二维码(转) - 生如夏花之灿烂 Android入门教程(三十四)------之多级树形菜单的实现 (转) Android入门教程(三十三)------之ListView自适应实现表格(转) Android入门教程(三十二)------之SQLite分页表格(转) Android入门教程(三十一)------SQLite分页读取(转) Android入门教程(三十)------之XML解析与生成(转) - 生如夏花之灿烂 Android入门教程(二十九)------之BroadcastReceiver (转) Android入门教程(二十八)------之Service(转) Android入门教程(二十七)------之Style与Theme (转) Android入门教程(二十五)------之画图(转) Android入门教程(二十四)------之Gallery + ImageSwitcher(转) Android入门教程(二十三)------之Gallery(转) Android入门教程(二十二)------之TabHost,TabWidget(转) Android入门教程(二十一)------之PopupWindow (转) Android入门教程(二十)之--之AlertDialog(转)
Android入门教程(二十六)------之ActivityGroup + GridView 实现Tab分页标签(转)
生如夏花之灿烂 · 2011-08-11 · via 博客园 - 生如夏花之灿烂

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

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

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

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

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

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

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

  • SetNumColumns():必须要使用setNumColumns来设置列数,因为这个GridView只有一行,即所有的Item都在同一行,Item数量即为列数。
  • setSelector(new ColorDrawable(Color.TRANSPARENT)):把系统默认选中的背景色透明化,因为我们已经在BaseAdapter中加入了SetFocus()来改变选中的样式。 
  1. package com.ActivityGroupDemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ActivityGroup;  
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.IntentFilter;  
  9. import android.graphics.Color;  
  10. import android.graphics.drawable.ColorDrawable;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.view.Gravity;  
  14. import android.view.View;  
  15. import android.view.Window;  
  16. import android.view.ViewGroup.LayoutParams;  
  17. import android.widget.AdapterView;  
  18. import android.widget.GridView;  
  19. import android.widget.LinearLayout;  
  20. import android.widget.Toast;  
  21. import android.widget.AdapterView.OnItemClickListener;  
  22.  
  23.  
  24.  
  25.  
  26.   
  27. public class ActivityGroupDemo extends ActivityGroup {  
  28.   
  29.     private GridView gvTopBar;  
  30.     private ImageAdapter topImgAdapter;  
  31.     public LinearLayout container;  
  32.   
  33.       
  34.     int[] topbar_image_array = { R.drawable.topbar_home,  
  35.             R.drawable.topbar_user, R.drawable.topbar_shoppingcart,  
  36.             R.drawable.topbar_note };  
  37.   
  38.     @Override  
  39.     public void onCreate(Bundle savedInstanceState) {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.main);  
  42.         gvTopBar = (GridView) this.findViewById(R.id.gvTopBar);  
  43.         gvTopBar.setNumColumns(topbar_image_array.length);  
  44.         gvTopBar.setSelector(new ColorDrawable(Color.TRANSPARENT));  
  45.         gvTopBar.setGravity(Gravity.CENTER);  
  46.         gvTopBar.setVerticalSpacing(0);  
  47.         int width = this.getWindowManager().getDefaultDisplay().getWidth()  
  48.                 / topbar_image_array.length;  
  49.         topImgAdapter = new ImageAdapter(this, topbar_image_array, width, 48,  
  50.                 R.drawable.topbar_itemselector);  
  51.         gvTopBar.setAdapter(topImgAdapter);  
  52.         gvTopBar.setOnItemClickListener(new ItemClickEvent());  
  53.         container = (LinearLayout) findViewById(R.id.Container);  
  54.         SwitchActivity(0);  
  55.     }  
  56.   
  57.     class ItemClickEvent implements OnItemClickListener {  
  58.   
  59.         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  60.                 long arg3) {  
  61.             SwitchActivity(arg2);  
  62.         }  
  63.     }  
  64.      
  65.  
  66.  
  67.   
  68.     void SwitchActivity(int id)  
  69.     {  
  70.         topImgAdapter.SetFocus(id);  
  71.         container.removeAllViews();  
  72.         Intent intent =null;  
  73.         if (id == 0 || id == 2) {  
  74.             intent = new Intent(ActivityGroupDemo.this, ActivityA.class);  
  75.         } else if (id == 1 || id == 3) {  
  76.             intent = new Intent(ActivityGroupDemo.this, ActivityB.class);  
  77.         }   
  78.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  79.           
  80.         Window subActivity = getLocalActivityManager().startActivity(  
  81.                 "subActivity", intent);  
  82.           
  83.         container.addView(subActivity.getDecorView(),  
  84.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  85.     }  
  86.   
  87. }  

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

  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>