- 浏览: 93994 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sincerehui:
android二维码的编码与解码(图片解码与摄像头解码) -
nyh1006:
学习一下!
UIScrollView循环滚动 -
bravewly:
为啥扫不出结果呢?是我扫的方式不对么?
android二维码的编码与解码(图片解码与摄像头解码) -
messigoogle:
我说您这个也是跟eoe上的差不多吧,加载的Assert中的图片 ...
android瀑布流 -
gundumw100:
LazyScrollView是偶写的。欢迎使用。呵呵。http ...
android瀑布流
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!
很多客户端软件和浏览器软件都喜欢用Tab分页标签来搭建界面框架。读者也许会马上想到使用TabHost 与 TabActivity的组合,其实最常用的不是它们,而是由GridView与ActivityGroup的组合。每当用户在GridView选中一项,ActivityGroup就把该项对应的Activity的Window作为View添加到ActivityGroup所指定的容器(LinearLayout)中。
接下来贴出本例运行的效果图:
ImageAdapter是本实例的关键之一,它继承于BaseAdapter,并加入一些自定义的方法。ImageAdapter的源码如下:
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()来改变选中的样式。
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文件源码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:layout_height="fill_parent"
android:layout_width="fill_parent">
<GridView android:layout_height="wrap_content" android:id="@+id/gvTopBar"
android:layout_alignParentTop="true" android:layout_width="fill_parent"
android:fadingEdgeLength="5dip" android:fadingEdge="vertical">
</GridView>
<LinearLayout android:id="@+id/Container"
android:layout_below="@+id/gvTopBar" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</RelativeLayout>
</LinearLayout>
发表评论
-
Android入门第三篇之RelativeLayout、FrameLayout
2011-10-17 08:41 630本文来自http://blog.csdn.net/hellog ... -
Android入门第一篇
2011-10-13 09:08 649本文来自http://blog.csdn.net/hellog ... -
Android入门第十六篇之Style与Theme
2011-10-13 09:06 761本文来自http://blog.csdn.net/hellog ... -
Android入门第十四篇之画图
2011-10-13 08:54 760本文来自http://blog.csdn.net/hellog ... -
Android入门第十三篇之Gallery + ImageSwitcher
2011-10-13 08:53 720本文来自http://blog.csdn.net/hel ... -
Android入门第十二篇之Gallery
2011-10-12 10:07 785本文来自http://blog.csdn.net/hello ... -
Android入门第十一篇之TabHost,TabWidget
2011-10-12 10:04 964本文来自http://blog.csdn.net/hellog ... -
Android入门第十篇之PopupWindow
2011-10-12 09:57 676本文来自http://blog.csdn.net/hellog ... -
Android入门第九篇之AlertDialog
2011-10-12 09:56 676本文来自http://blog.csdn.net/hellog ... -
Android入门第八篇之GridView(九宫图)
2011-10-12 00:08 825本文来自http://blog.csdn.net/hellog ... -
Android入门第七篇之ListView (二)
2011-10-10 13:36 922本文来自http://blog.csdn.net/hellog ... -
Android入门第六篇之ListView (一)
2011-10-10 13:33 674本文来自http://blog.csdn.net/hellog ... -
android去掉标题的方法
2011-10-10 09:40 729<?xml version="1.0" ... -
Android入门第五篇之TableLayout (二)
2011-09-27 11:26 783本文来自http://blog.csdn.net/hellog ... -
Android入门第四篇之TableLayout (一)
2011-09-27 11:23 716本文来自http://blog.csdn.net/hell ... -
Android入门第二篇之LinearLayout、AbsoluteLayout
2011-09-27 11:03 691Android 的UI 布局都以Layout 作为容器,在上面 ...
相关推荐
ActivityGroup + GridView 实现Tab分页标签
用ActivityGroup+GridView实现Tab分页标签,并且可以滑动切换页面
很多客户端软件和浏览器软件都喜欢用Tab分页标签来搭建界面框架。读者也许会马上想到使用TabHost 与 TabActivity的组合,其实最常用的不是它们,而是由GridView与ActivityGroup的组合。每当用户在GridView选中一项,...
主要介绍了Android入门之ActivityGroup+GridView实现Tab分页标签的方法,非常实用的功能,需要的朋友可以参考下
http://blog.csdn.net/lyl_studio/article/details/7830487 示例源码
使用GridView+ActivityGroup实现的tab分页效果
基于GridView和ActivityGroup实现的Tab分页
GridView+ActivityGroup 实现Tab分页
今天为大家介绍下使用GridView和ActivityGroup实现的分页,这里需要将Activity转换成Window,然后再换成成View添加到容器中,具体实现代码如下,感兴趣的朋友可以参考下哈
我之前做分页都是用GridView和ActivityGroup实现的 因为觉得TabHost不好用 还有就是自己水平差的原因吧 如果帮的话 重写view任何问题都可以解决 呵呵 下面请看实现过程 其实很简单