package com.example.ghost.text1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by GHOST on 2017/1/28.
* Fragment
*/
public class tabActivity extends FragmentActivity implements View.OnClickListener {
// 四个tab布局
private LinearLayout takeOutLayout, expressLayout, personLayout, nearbyLayout;
// 底部标签切换的Fragment
private Fragment takeOutFragment, expressFragment, personFragment, nearbyFragment;
// 底部标签图片
private ImageView takeOutImg, expressImg, nearbyImg, personImg;
private TextView takeoutTv, expressTv, nearbyTv, presonTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_menu);
initView();
initEvent();
setSelect(0);
}
private void initEvent() {
takeOutLayout.setOnClickListener(this);
expressLayout.setOnClickListener(this);
nearbyLayout.setOnClickListener(this);
personLayout.setOnClickListener(this);
}
private void initView() {
takeOutLayout = (LinearLayout) findViewById(R.id.id_tab_takeout);
expressLayout = (LinearLayout) findViewById(R.id.id_tab_express);
nearbyLayout = (LinearLayout) findViewById(R.id.id_tab_nearby);
personLayout = (LinearLayout) findViewById(R.id.id_tab_person);
takeOutImg = (ImageView) findViewById(R.id.id_tab_takeout_img);
expressImg = (ImageView) findViewById(R.id.id_tab_express_img);
nearbyImg = (ImageView) findViewById(R.id.id_tab_nearby_img);
personImg = (ImageView) findViewById(R.id.id_tab_person_img);
}
private void setSelect(int i) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
hideFragment(transaction);
// 把图片设置为亮的
// 设置内容区域
switch (i) {
case 0:
if (takeOutFragment == null) {
takeOutFragment = new tab1_fragment();
transaction.add(R.id.id_content, takeOutFragment);
} else {
transaction.show(takeOutFragment);
}
takeOutImg.setImageResource(R.drawable.tab_1);
break;
case 1:
if (expressFragment == null) {
expressFragment = new tab2_fragment();
transaction.add(R.id.id_content, expressFragment);
} else {
transaction.show(expressFragment);
}
expressImg.setImageResource(R.drawable.tab_2);
break;
case 2:
if (nearbyFragment == null) {
nearbyFragment = new tab3_fragment();
transaction.add(R.id.id_content, nearbyFragment);
} else {
transaction.show(nearbyFragment);
}
nearbyImg.setImageResource(R.drawable.tab_3);
break;
case 3:
if (personFragment == null) {
personFragment = new tab4_fragment();
transaction.add(R.id.id_content, personFragment);
} else {
transaction.show(personFragment);
}
personImg.setImageResource(R.drawable.tab_4);
break;
default:
break;
}
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction) {
if (takeOutFragment != null) {
transaction.hide(takeOutFragment);
}
if (expressFragment != null) {
transaction.hide(expressFragment);
}
if (nearbyFragment != null) {
transaction.hide(nearbyFragment);
}
if (personFragment != null) {
transaction.hide(personFragment);
}
}
@Override
public void onClick(View v) {
resetImgs();
switch (v.getId()) {
case R.id.id_tab_takeout:
setSelect(0);
break;
case R.id.id_tab_express:
setSelect(1);
break;
case R.id.id_tab_nearby:
setSelect(2);
break;
case R.id.id_tab_person:
setSelect(3);
break;
default:
break;
}
}
private void resetImgs() {
takeOutImg.setImageResource(R.drawable.tab_1_1);
expressImg.setImageResource(R.drawable.tab_2_2);
nearbyImg.setImageResource(R.drawable.tab_3_3);
personImg.setImageResource(R.drawable.tab_4_4);
}
}