/**
* 获取界面上的UI控件
*/
private void init() {
tv_back = (TextView) findViewById(R.id.tv_back);
tv_main_title = (TextView) findViewById(R.id.tv_main_title);
tv_main_title.setText("Fangtaobap");
rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
rl_title_bar.setBackgroundColor(Color.parseColor("#FF8000"));
tv_back.setVisibility(View.GONE);
initBodyLayout();
}
/**
* 获取底部导航栏上的控件
*/
private void initBottomBar() {
mBottomLayout = (LinearLayout) findViewById(R.id.main_bottom_bar);
mHomeBtn = findViewById(R.id.bottom_bar_home_btn);
mClassifyBtn = findViewById(R.id.bottom_bar_classify_btn);
mShopcarBtn = findViewById(R.id.bottom_bar_shopcar_btn);
mMyInfoBtn = findViewById(R.id.bottom_bar_myinfo_btn);
tv_home = (TextView) findViewById(R.id.bottom_bar_text_home);
tv_classify = (TextView) findViewById(R.id.bottom_bar_text_classify);
tv_shopcar = (TextView) findViewById(R.id.bottom_bar_text_shopcar);
tv_myInfo = (TextView) findViewById(R.id.bottom_bar_text_myinfo);
iv_home = (ImageView) findViewById(R.id.bottom_bar_image_home);
iv_classify = (ImageView) findViewById(R.id.bottom_bar_image_classify);
iv_shopcar = (ImageView) findViewById(R.id.bottom_bar_image_shopcar);
iv_myInfo = (ImageView) findViewById(R.id.bottom_bar_image_myinfo);
}
private void initBodyLayout() {
mBodyLayout = (FrameLayout) findViewById(R.id.main_body);
}
/**
* 控件的点击事件
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
//首页的点击事件
case R.id.bottom_bar_home_btn:
clearBottomImageState();
selectDisplayView(0);
break;
//分类的点击事件
case R.id.bottom_bar_classify_btn:
clearBottomImageState();
selectDisplayView(1);
break;
//购物车的点击事件
case R.id.bottom_bar_shopcar_btn:
clearBottomImageState();
selectDisplayView(2);
break;
//我的点击事件
case R.id.bottom_bar_myinfo_btn:
clearBottomImageState();
selectDisplayView(3);
break;
default:
break;
}
}
/**
* 设置底部四个按钮的点击监听事件
*/
private void setListener() {
for (int i = 0; i < mBottomLayout.getChildCount(); i++) {
mBottomLayout.getChildAt(i).setOnClickListener(this);
}
}
/**
* 清除底部按钮的选中状态
*/
private void clearBottomImageState() {
tv_home.setTextColor(Color.parseColor("#666666"));
tv_classify.setTextColor(Color.parseColor("#666666"));
tv_shopcar.setTextColor(Color.parseColor("#666666"));
tv_myInfo.setTextColor(Color.parseColor("#666666"));
iv_home.setImageResource(R.mipmap.main_homepage_icon);
iv_classify.setImageResource(R.mipmap.main_classify);
iv_shopcar.setImageResource(R.mipmap.main_shopcar_icon);
iv_myInfo.setImageResource(R.mipmap.main_my_icon);
for (int i = 0; i < mBottomLayout.getChildCount(); i++) {
mBottomLayout.getChildAt(i).setSelected(false);
}
}
/**
* 设置底部按钮选中状态
*/
public void setSelectedStatus(int index) {
switch (index) {
case 0:
mHomeBtn.setSelected(true);
iv_home.setImageResource(R.mipmap.main_homepage_icon_selected);
tv_home.setTextColor(Color.parseColor("#0097F7"));
rl_title_bar.setVisibility(View.VISIBLE);
tv_main_title.setText("首页");
break;
case 1:
mClassifyBtn.setSelected(true);
iv_classify.setImageResource(R.mipmap.main_classify_selected);
tv_classify.setTextColor(Color.parseColor("#0097F7"));
rl_title_bar.setVisibility(View.VISIBLE);
tv_main_title.setText("分类");
break;
case 2:
mShopcarBtn.setSelected(true);
iv_shopcar.setImageResource(R.mipmap.main_shopcar_icon_selected);
tv_shopcar.setTextColor(Color.parseColor("#0097F7"));
rl_title_bar.setVisibility(View.VISIBLE);
tv_main_title.setText("购物车");
break;
case 3:
mMyInfoBtn.setSelected(true);
iv_myInfo.setImageResource(R.mipmap.main_my_icon_selected);
tv_myInfo.setTextColor(Color.parseColor("#0097F7"));
rl_title_bar.setVisibility(View.GONE);
}
}
/**
* 移除不需要的视图
*/
private void removeAllView() {
for (int i = 0; i < mBodyLayout.getChildCount(); i++) {
mBodyLayout.getChildAt(i).setVisibility(View.GONE);
}
}
/**
* 设置界面view的初始化状态
*/
private void setInitStatus() {
clearBottomImageState();
setSelectedStatus(0);
createView(0);
}
/**
* 显示对应的页面
*/
private void selectDisplayView(int index) {
removeAllView();
createView(index);
setSelectedStatus(index);
}
/**
* 选择视图
*/
private void createView(int viewIndex) {
switch (viewIndex) {
case 0:
//首页
if (homeView == null) {
homeView = new HomeView(this);
mBodyLayout.addView(homeView.getView());
} else {
homeView.getView();
}
homeView.showView();
break;
case 1:
//分类
if (typeView == null) {
typeView = new TypeView(this);
mBodyLayout.addView(typeView.getView());
} else {
typeView.getView();
}
typeView.showView();
break;
case 2:
//购物车
if (shopingView == null) {
shopingView = new ShopingView(this);
mBodyLayout.addView(shopingView.getView());
} else {
shopingView.getView();
}
shopingView.showView();
break;
case 3:
//我的
if (mMyInfoView == null) {
mMyInfoView = new MyInfoView(this);
mBodyLayout.addView(mMyInfoView.getView());
} else {
mMyInfoView.getView();
}
mMyInfoView.showView();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
//从设置界面或登录界面传递过来的登录状态
boolean isLogin = data.getBooleanExtra("isLogin", false);
if (isLogin) {//登录成功时显示首页
clearBottomImageState();
selectDisplayView(0);
}
if (mMyInfoView != null) {//登录成功或退出登录时根据isLogin设置我的界面
mMyInfoView.setLoginParams(isLogin);
}
}
}
protected long exitTime;//记录第一次点击时的时间
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN) {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(MainActivity.this, "再按一次退出Fangtaobao",
Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
MainActivity.this.finish();
if (readLoginStatus()) {
//如果退出此应用时是登录状态,则需要清除登录状态,同时需清除登录时的用户名
clearLoginStatus();
}
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* 获取SharedPreferences中的登录状态
*/
private boolean readLoginStatus() {
SharedPreferences sp = getSharedPreferences("loginInfo",
Context.MODE_PRIVATE);
boolean isLogin = sp.getBoolean("isLogin", false);
return isLogin;
}
/**
* 清除SharedPreferences中的登录状态
*/
private void clearLoginStatus() {
SharedPreferences sp = getSharedPreferences("loginInfo",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();//获取编辑器
editor.putBoolean("isLogin", false);//清除登录状态
editor.putString("loginUserName", "");//清除登录时的用户名
editor.commit();//提交修改
}