package com.xiaoqiu.ui;
import com.xiaoqiu.adapter.ImageAdapter;
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.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
/**
* 主要是根据点最上方导航栏来决定显示哪个Activity
* @author Tian
*
*/
@SuppressWarnings("deprecation")
public class MainActivity extends ActivityGroup {
private GridView gvTopBar;
private ImageAdapter topImgAdapter;
public LinearLayout container;// 装载sub Activity的容器
/** 顶部按钮图片 **/
int[] topbar_image_array = { R.drawable.frame_player_normal,
R.drawable.frame_local_normal, R.drawable.frame_internet_normal,
R.drawable.frame_menu_normal };
@Override
public void onCreate(Bundle savedInstanceState) {
setFullscreen();
setNoTitle();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView()
{
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, 100,
R.drawable.frame_internet_press);
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<?> parent, View view, int i,
long l) {
SwitchActivity(i);
}
}
/**
* 根据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(MainActivity.this, ProfilesActivity.class);
} else if (id == 1 || id == 3) {
intent = new Intent(MainActivity.this, RuleActivity.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);
}
//全屏
public void setFullscreen() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
//无标题
public void setNoTitle() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
package com.xiaoqiu.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/**
*
* @author tian
*@version
*/
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].setPadding(2, 2, 2, 2);
imgItems[i].setImageResource(picIds[i]);
}
}
@Override
public int getCount() {
return imgItems.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
/**
* 设置选中的效果
* 当选中选项A 则A设置成什么样式 其余的又设置成其余的样式
*/
public void SetFocus(int index)
{
for(int i=0;i<imgItems.length;i++)
{
if(i!=index)
{
imgItems[i].setBackgroundResource(0);//恢复未选中的样式
}
}
imgItems[index].setBackgroundResource(selResId);//设置选中的样式
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView=imgItems[position];
}else{
imageView = (ImageView) convertView;
}
return imageView;
}
}