package com.yangyu.mycustomtab03;

import android.content.Context;

import android.graphics.drawable.BitmapDrawable;

import android.os.Bundle;

import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentTransaction;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnTouchListener;

import android.widget.FrameLayout;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.PopupWindow;

import android.widget.PopupWindow.OnDismissListener;

public class MainActivity extends FragmentActivity implements OnClickListener{

//定义Fragment页面

private FragmentPage1 fragmentPage1;

private FragmentPage2 fragmentPage2;

private FragmentPage3 fragmentPage3;

private FragmentPage4 fragmentPage4;

//定义布局对象

private FrameLayout friendfeedFl,myfeedFl,homeFl,moreFl;

//定义图片组件对象

private ImageView friendfeedIv,myfeedIv,homeIv,moreIv;

//定义按钮图片组件

private ImageView toggleImageView,plusImageView;

//定义PopupWindow

private PopupWindow popWindow;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initData();

//初始化默认为选中点击了“动态”按钮

clickFriendfeedBtn();

}

/**

* 初始化组件

*/

private void initView(){

//实例化布局对象

friendfeedFl = (FrameLayout)findViewById(R.id.layout_friendfeed);

myfeedFl = (FrameLayout)findViewById(R.id.layout_myfeed);

homeFl = (FrameLayout)findViewById(R.id.layout_home);

moreFl = (FrameLayout)findViewById(R.id.layout_more);

//实例化图片组件对象

friendfeedIv = (ImageView)findViewById(R.id.image_friendfeed);

myfeedIv = (ImageView)findViewById(R.id.image_myfeed);

homeIv = (ImageView)findViewById(R.id.image_home);

moreIv = (ImageView)findViewById(R.id.image_more);

//实例化按钮图片组件

toggleImageView = (ImageView)findViewById(R.id.toggle_btn);

plusImageView = (ImageView)findViewById(R.id.plus_btn);

}

/**

* 初始化数据

*/

private void initData(){

//给布局对象设置监听

friendfeedFl.setOnClickListener(this);

myfeedFl.setOnClickListener(this);

homeFl.setOnClickListener(this);

moreFl.setOnClickListener(this);

//给按钮图片设置监听

toggleImageView.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

//点击动态按钮

case R.id.layout_friendfeed:

clickFriendfeedBtn();

break;

//点击与我相关按钮

case R.id.layout_myfeed:

clickMyfeedBtn();

break;

//点击我的空间按钮

case R.id.layout_home:

clickHomeBtn();

break;

//点击更多按钮

case R.id.layout_more:

clickMoreBtn();

break;

//点击中间按钮

case R.id.toggle_btn:

clickToggleBtn();

break;

}

}

/**

* 显示PopupWindow弹出菜单

*/

private void showPopupWindow(View parent){

if (popWindow == null) {

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = layoutInflater.inflate(R.layout.popwindow_layout,null);

// 创建一个PopuWidow对象

popWindow = new PopupWindow(view,LinearLayout.LayoutParams.FILL_PARENT, 200);

}

// 使其聚集 ,要想监听菜单里控件的事件就必须要调用此方法

popWindow.setFocusable(true);

// 设置允许在外点击消失

popWindow.setOutsideTouchable(true);

// 设置背景,这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景

popWindow.setBackgroundDrawable(new BitmapDrawable());

//设置菜单显示的位置

popWindow.showAsDropDown(parent, Gravity.CENTER, 0);

//监听菜单的关闭事件

popWindow.setOnDismissListener(new OnDismissListener() {

@Override

public void onDismiss() {

//改变显示的按钮图片为正常状态

changeButtonImage();

}

});

//监听触屏事件

popWindow.setTouchInterceptor(new OnTouchListener() {

public boolean onTouch(View view, MotionEvent event) {

//改变显示的按钮图片为正常状态

changeButtonImage();

return false;

}

});

}

/**

* 点击了“动态”按钮

*/

private void clickFriendfeedBtn(){

//实例化Fragment页面

fragmentPage1 = new FragmentPage1();

//得到Fragment事务管理器

FragmentTransaction        fragmentTransaction = this.getSupportFragmentManager().beginTransaction();

//替换当前的页面

fragmentTransaction.replace(R.id.frame_content, fragmentPage1);

//事务管理提交

fragmentTransaction.commit();

friendfeedFl.setSelected(true);

friendfeedIv.setSelected(true);

myfeedFl.setSelected(false);

myfeedIv.setSelected(false);

homeFl.setSelected(false);

homeIv.setSelected(false);

moreFl.setSelected(false);

moreIv.setSelected(false);

}

/**

* 点击了“与我相关”按钮

*/

private void clickMyfeedBtn(){

// 实例化Fragment页面

fragmentPage2 = new FragmentPage2();

// 得到Fragment事务管理器

FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();

// 替换当前的页面

fragmentTransaction.replace(R.id.frame_content, fragmentPage2);

// 事务管理提交

fragmentTransaction.commit();

friendfeedFl.setSelected(false);

friendfeedIv.setSelected(false);

myfeedFl.setSelected(true);

myfeedIv.setSelected(true);

homeFl.setSelected(false);

homeIv.setSelected(false);

moreFl.setSelected(false);

moreIv.setSelected(false);

}

/**

* 点击了“我的空间”按钮

*/

private void clickHomeBtn(){

// 实例化Fragment页面

fragmentPage3 = new FragmentPage3();

// 得到Fragment事务管理器

FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();

// 替换当前的页面

fragmentTransaction.replace(R.id.frame_content, fragmentPage3);

// 事务管理提交

fragmentTransaction.commit();

friendfeedFl.setSelected(false);

friendfeedIv.setSelected(false);

myfeedFl.setSelected(false);

myfeedIv.setSelected(false);

homeFl.setSelected(true);

homeIv.setSelected(true);

moreFl.setSelected(false);

moreIv.setSelected(false);

}

/**

* 点击了“更多”按钮

*/

private void clickMoreBtn(){

// 实例化Fragment页面

fragmentPage4 = new FragmentPage4();

// 得到Fragment事务管理器

FragmentTransaction fragmentTransaction = this.getSupportFragmentManager().beginTransaction();

// 替换当前的页面

fragmentTransaction.replace(R.id.frame_content, fragmentPage4);

// 事务管理提交

fragmentTransaction.commit();

friendfeedFl.setSelected(false);

friendfeedIv.setSelected(false);

myfeedFl.setSelected(false);

myfeedIv.setSelected(false);

homeFl.setSelected(false);

homeIv.setSelected(false);

moreFl.setSelected(true);

moreIv.setSelected(true);

}

/**

* 点击了中间按钮

*/

private void clickToggleBtn(){

showPopupWindow(toggleImageView);

//改变按钮显示的图片为按下时的状态

plusImageView.setImageResource(R.drawable.toolbar_plusback);

toggleImageView.setImageResource(R.drawable.toolbar_btn_pressed);

}

/**

* 改变显示的按钮图片为正常状态

*/

private void changeButtonImage(){

plusImageView.setImageResource(R.drawable.toolbar_plus);

toggleImageView.setImageResource(R.drawable.toolbar_btn_normal);

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐