打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ActivityGroup 实现分页和自定义标签(内有GridView的点击背景样式的改变...

ActivityGroup 实现分页和自定义标签(内有GridView的点击背景样式的改变方法)

有关基础的ActivityGroup 可以去看这个博客

http://blog.csdn.net/hellogv/article/details/6057174

我这里实现的是方法和这个帖子的主要差别的就是界面都是自定义的。这样可以实现很多美观的分页,新浪微博等的效果一样可以达到。上效果图如下:

可以看到下方的就是标签界面了,这是一个GridVIew。很多人都说设置GridVIew的list setselector属性,但是这个属性可是改变点击后一瞬间的背景样式。所以最好的方法还是点击的position记录下来进行,进行界面的更新。


TestGroupActivity.java代码:

  1. package com.renrenwei.activity;  
  2.   
  3. import com.renrenwei.adapter.ActivityGroupImageAdapter;  
  4.   
  5. import android.app.ActivityGroup;  
  6. import android.content.Intent;  
  7. import android.graphics.Color;  
  8. import android.graphics.drawable.ColorDrawable;  
  9. import android.os.Bundle;  
  10. import android.view.Gravity;  
  11. import android.view.View;  
  12. import android.view.ViewGroup.LayoutParams;  
  13. import android.view.Window;  
  14. import android.widget.AdapterView;  
  15. import android.widget.AdapterView.OnItemClickListener;  
  16. import android.widget.GridView;  
  17. import android.widget.LinearLayout;  
  18.   
  19. public class TestActivityGroup extends ActivityGroup {  
  20.     private int barnum = 4;  
  21.     private GridView bottombar;  
  22.     private ActivityGroupImageAdapter bottomadapter;  
  23.     private LinearLayout showActivityView;  
  24.   
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题  
  28.         setContentView(R.layout.activitygroup);  
  29.         bottombar = (GridView) this.findViewById(R.id.gridview_bottombar);  
  30.         bottombar.setNumColumns(barnum);  
  31.         bottombar.setSelector(new ColorDrawable(Color.TRANSPARENT));// 设置选中为透明色  
  32.         bottombar.setGravity(Gravity.CENTER);// 位置居中  
  33.         bottombar.setVerticalSpacing(0);// 垂直间隔  
  34.         bottomadapter = new ActivityGroupImageAdapter(this, barnum);  
  35.         bottombar.setAdapter(bottomadapter);  
  36.         bottombar.setOnItemClickListener(new BottomListener());  
  37.         showActivityView = (LinearLayout) this  
  38.                 .findViewById(R.id.group_showview);  
  39.         SwitchActivity(0);// 默认打开的界面。  
  40.     }  
  41.   
  42.     private void SwitchActivity(int arg2) {  
  43.         bottomadapter.SetFocus(arg2);  
  44.         showActivityView.removeAllViews();// 必须先清除容器中所有的View  
  45.         Intent intent = null;  
  46.         int itemnum = arg2;  
  47.         if (itemnum == 0) {  
  48.             intent = new Intent(TestActivityGroup.this,  
  49.                     TestRenRenWeiActivity.class);  
  50.         }  
  51.         if (itemnum == 1) {  
  52.             intent = new Intent(TestActivityGroup.this, TestMyActivity.class);  
  53.         }  
  54.         if (itemnum == 2) {  
  55.             intent = new Intent(TestActivityGroup.this,  
  56.                     TestRenRenWeiActivity.class);  
  57.         }  
  58.         if (itemnum == 3) {  
  59.             intent = new Intent(TestActivityGroup.this,  
  60.                     TestRenRenWeiActivity.class);  
  61.         }  
  62.         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  63.         Window subActivity = getLocalActivityManager().startActivity(  
  64.                 "subActivity", intent);  
  65.         // 容器添加View  
  66.         showActivityView.addView(subActivity.getDecorView(),  
  67.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  68.     }  
  69.   
  70.     class BottomListener implements OnItemClickListener {  
  71.   
  72.         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  73.                 long arg3) {  
  74.             SwitchActivity(arg2);  
  75.         }  
  76.     }  
  77. }  

ActivityGroupAdapter,java代码:
  1. package com.renrenwei.adapter;  
  2.   
  3. import com.renrenwei.activity.R;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12.   
  13. public class ActivityGroupImageAdapter extends BaseAdapter {  
  14.     private int bottomnum;  
  15.     private LayoutInflater layoutinflater;  
  16.     private View myview;  
  17.     private int index = 0;  
  18.     public ActivityGroupImageAdapter(Context c, int bottomnum) {  
  19.         this.bottomnum = bottomnum;  
  20.         this.layoutinflater = LayoutInflater.from(c);  
  21.     }  
  22.     public int getCount() {  
  23.         return bottomnum;  
  24.     }  
  25.   
  26.     public void SetFocus(int index) {  
  27.         this.index = index;  
  28. //      this.notifyDataSetChanged();  
  29.         this.notifyDataSetInvalidated();//刷新界面  
  30.     }  
  31.   
  32.     public Object getItem(int position) {  
  33.         return position;  
  34.     }  
  35.   
  36.     public long getItemId(int position) {  
  37.         return position;  
  38.     }  
  39.   
  40.     public View getView(int position, View convertView, ViewGroup parent) {  
  41.         myview = layoutinflater.inflate(R.layout.group_bottom_item, null);  
  42.         ImageView imageview = (ImageView) myview.findViewById(R.id.imageview_bottom);  
  43.         TextView textview = (TextView) myview.findViewById(R.id.textview_bottom);  
  44.         System.out.println("第"+position+"次index="+index);  
  45.         if (position == 0) {  
  46.             imageview.setBackgroundResource(android.R.drawable.ic_menu_mapmode);  
  47.             textview.setText(R.string.Group_Item_Main);  
  48.         }  
  49.         if (position == 1) {  
  50.             imageview  
  51.                     .setBackgroundResource(android.R.drawable.ic_menu_myplaces);  
  52.             textview.setText(R.string.Group_Item_My);  
  53.         }  
  54.         if (position == 2) {  
  55.             imageview.setBackgroundResource(android.R.drawable.ic_menu_share);  
  56.             textview.setText(R.string.Group_Item_square);  
  57.         }  
  58.         if (position == 3) {  
  59.             imageview.setBackgroundResource(android.R.drawable.ic_menu_more);  
  60.             textview.setText(R.string.Group_Item_More);  
  61.         }  
  62.         if(position==index){  
  63.             myview.setBackgroundResource(R.drawable.view_yuan_login);  
  64.         }else{  
  65.             myview.setBackgroundResource(0);  
  66.         }  
  67.         return myview;  
  68.     }  
  69. }  

这里的group_bottom_item界面就是你自己定制的界面,在这里你可以加入一些你想加入的控件。如ImageView和TextView等等。

下面上界面源码,activitygroup.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  2.               android:layout_width="fill_parent"   
  3.               android:layout_height="fill_parent"   
  4.               android:orientation="vertical">  
  5.     <RelativeLayout android:layout_width="fill_parent"   
  6.                     android:layout_height="fill_parent">  
  7.                     <LinearLayout android:layout_width="fill_parent"   
  8.                                   android:layout_height="fill_parent"   
  9.                                   android:id="@+id/group_showview">  
  10.                     </LinearLayout>  
  11.                     <GridView android:id="@+id/gridview_bottombar"  
  12.                               android:layout_width="fill_parent"   
  13.                               android:layout_alignParentBottom="true"   
  14.                               android:fadingEdgeLength="5dip"  
  15.                               android:fadingEdge="vertical"  
  16.                               android:background="@color/Gray"   
  17.                               android:layout_height="80dip">  
  18.                     </GridView>  
  19.     </RelativeLayout>  
  20. </LinearLayout>  

group_bottom_item.xml
  1. <LinearLayout android:id="@+id/linearLayout1"  
  2.               android:layout_width="fill_parent"   
  3.               android:layout_height="fill_parent"  
  4.               xmlns:android="http://schemas.android.com/apk/res/android"   
  5.               android:orientation="vertical" android:gravity="center">  
  6.              <ImageView android:id="@+id/imageview_bottom"  
  7.                         android:layout_width="wrap_content"   
  8.                         android:layout_height="wrap_content"/>  
  9.              <TextView  android:id="@+id/textview_bottom"  
  10.                         android:layout_width="fill_parent"   
  11.                         android:layout_height="wrap_content"   
  12.                         android:gravity="center"   
  13.                         android:textColor="@color/White"/>  
  14. </LinearLayout>  

嘿嘿,一个自定义的ActivityGroup标签界面就做好啦。
  1. R.drawable.view_yuan_login  
这个是一个样式效果。如需要的可以自己去网上查找相关效果。


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java-阵列适配器从Res文件夹加载图像(Android App)
Android根据包名取得指定程序包的信息(名称、图标……) | 梦宇
Android 仿QQ主页面的实
第九讲:用户界面 View(四)
我的Android学习之旅[6]——以示例程序来展示Android的几种布局方式
Android ListView中item view重复使用带来的问题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服