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

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

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

我这里实现的是方法和这个帖子的主要差别的就是界面都是自定义的。这样可以实现很多美观的分页,新浪微博等的效果一样可以达到。上效果图如下:ActivityGroup 实现分页和自定义标签(内有GridView的点击背景样式的改变方法)

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


TestGroupActivity.java代码:

package com.renrenwei.activity;

import com.renrenwei.adapter.ActivityGroupImageAdapter;

import android.app.ActivityGroup;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;

public class TestActivityGroup extends ActivityGroup {
	private int barnum = 4;
	private GridView bottombar;
	private ActivityGroupImageAdapter bottomadapter;
	private LinearLayout showActivityView;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题
		setContentView(R.layout.activitygroup);
		bottombar = (GridView) this.findViewById(R.id.gridview_bottombar);
		bottombar.setNumColumns(barnum);
		bottombar.setSelector(new ColorDrawable(Color.TRANSPARENT));// 设置选中为透明色
		bottombar.setGravity(Gravity.CENTER);// 位置居中
		bottombar.setVerticalSpacing(0);// 垂直间隔
		bottomadapter = new ActivityGroupImageAdapter(this, barnum);
		bottombar.setAdapter(bottomadapter);
		bottombar.setOnItemClickListener(new BottomListener());
		showActivityView = (LinearLayout) this
				.findViewById(R.id.group_showview);
		SwitchActivity(0);// 默认打开的界面。
	}

	private void SwitchActivity(int arg2) {
		bottomadapter.SetFocus(arg2);
		showActivityView.removeAllViews();// 必须先清除容器中所有的View
		Intent intent = null;
		int itemnum = arg2;
		if (itemnum == 0) {
			intent = new Intent(TestActivityGroup.this,
					TestRenRenWeiActivity.class);
		}
		if (itemnum == 1) {
			intent = new Intent(TestActivityGroup.this, TestMyActivity.class);
		}
		if (itemnum == 2) {
			intent = new Intent(TestActivityGroup.this,
					TestRenRenWeiActivity.class);
		}
		if (itemnum == 3) {
			intent = new Intent(TestActivityGroup.this,
					TestRenRenWeiActivity.class);
		}
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Window subActivity = getLocalActivityManager().startActivity(
				"subActivity", intent);
		// 容器添加View
		showActivityView.addView(subActivity.getDecorView(),
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	}

	class BottomListener implements OnItemClickListener {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			SwitchActivity(arg2);
		}
	}
}

ActivityGroupAdapter,java代码:

package com.renrenwei.adapter;

import com.renrenwei.activity.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ActivityGroupImageAdapter extends BaseAdapter {
	private int bottomnum;
	private LayoutInflater layoutinflater;
	private View myview;
	private int index = 0;
	public ActivityGroupImageAdapter(Context c, int bottomnum) {
		this.bottomnum = bottomnum;
		this.layoutinflater = LayoutInflater.from(c);
	}
	public int getCount() {
		return bottomnum;
	}

	public void SetFocus(int index) {
		this.index = index;
//		this.notifyDataSetChanged();
		this.notifyDataSetInvalidated();//刷新界面
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		myview = layoutinflater.inflate(R.layout.group_bottom_item, null);
		ImageView imageview = (ImageView) myview.findViewById(R.id.imageview_bottom);
		TextView textview = (TextView) myview.findViewById(R.id.textview_bottom);
		System.out.println("第"+position+"次index="+index);
		if (position == 0) {
			imageview.setBackgroundResource(android.R.drawable.ic_menu_mapmode);
			textview.setText(R.string.Group_Item_Main);
		}
		if (position == 1) {
			imageview
					.setBackgroundResource(android.R.drawable.ic_menu_myplaces);
			textview.setText(R.string.Group_Item_My);
		}
		if (position == 2) {
			imageview.setBackgroundResource(android.R.drawable.ic_menu_share);
			textview.setText(R.string.Group_Item_square);
		}
		if (position == 3) {
			imageview.setBackgroundResource(android.R.drawable.ic_menu_more);
			textview.setText(R.string.Group_Item_More);
		}
		if(position==index){
			myview.setBackgroundResource(R.drawable.view_yuan_login);
		}else{
			myview.setBackgroundResource(0);
		}
		return myview;
	}
}

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

下面上界面源码,activitygroup.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
			  android:layout_width="fill_parent" 
			  android:layout_height="fill_parent" 
			  android:orientation="vertical">
	<RelativeLayout android:layout_width="fill_parent" 
					android:layout_height="fill_parent">
				    <LinearLayout android:layout_width="fill_parent" 
				    			  android:layout_height="fill_parent" 
				    			  android:id="@+id/group_showview">
				    </LinearLayout>
				    <GridView android:id="@+id/gridview_bottombar"
				    		  android:layout_width="fill_parent" 
				    	  	  android:layout_alignParentBottom="true" 
				    	  	  android:fadingEdgeLength="5dip"
				    	  	  android:fadingEdge="vertical"
				    	  	  android:background="@color/Gray" 
				    	  	  android:layout_height="80dip">
				    </GridView>
	</RelativeLayout>
</LinearLayout>

group_bottom_item.xml

<LinearLayout android:id="@+id/linearLayout1"
			  android:layout_width="fill_parent" 
			  android:layout_height="fill_parent"
			  xmlns:android="http://schemas.android.com/apk/res/android" 
			  android:orientation="vertical" android:gravity="center">
			 <ImageView android:id="@+id/imageview_bottom"
			 			android:layout_width="wrap_content" 
			 			android:layout_height="wrap_content"/>
			 <TextView  android:id="@+id/textview_bottom"
			 			android:layout_width="fill_parent" 
			 			android:layout_height="wrap_content" 
			 			android:gravity="center" 
			 			android:textColor="@color/White"/>
</LinearLayout>

嘿嘿,一个自定义的ActivityGroup标签界面就做好啦。

R.drawable.view_yuan_login
这个是一个样式效果。如需要的可以自己去网上查找相关效果。





原文链接: http://blog.csdn.net/peijiangping1989/article/details/6731489

你可能感兴趣的:(ActivityGroup 实现分页和自定义标签(内有GridView的点击背景样式的改变方法))