fragment搭主界面
public class MainActivity extends AppCompatActivity {
private Context ctx = this;
private View ll_tab0;
private View ll_tab1;
private View ll_tab2;
private View ll_tab3;
private ImageView iv_tab0;
private ImageView iv_tab1;
private ImageView iv_tab2;
private ImageView iv_tab3;
private TextView tv_tab0;
private TextView tv_tab1;
private TextView tv_tab2;
private TextView tv_tab3;
private FrameLayout fl_content;
private MainFragment mainFragment;
private FujinFragment fujinFragment;
private JiekeFragment jiekeFragment;
private WodeFragment wodeFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化ViewsById
initViewsById();
}
//==============================================================================================
//初始化ViewsById
private void initViewsById() {
//初始化FrameLayout
initFrameLayout();
//初始化底部tab
initBottomTab();
}
//==============================================================================================
//初始化FrameLayout
private void initFrameLayout() {
fl_content = (FrameLayout) findViewById(R.id.fl_content);
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fl_content, mainFragment, "main").commitAllowingStateLoss();
}
//==============================================================================================
//初始化底部tab
private void initBottomTab() {
ll_tab0 = findViewById(R.id.ll_tab0);
ll_tab1 = findViewById(R.id.ll_tab1);
ll_tab2 = findViewById(R.id.ll_tab2);
ll_tab3 = findViewById(R.id.ll_tab3);
iv_tab0 = (ImageView) findViewById(R.id.iv_tab0);
iv_tab1 = (ImageView) findViewById(R.id.iv_tab1);
iv_tab2 = (ImageView) findViewById(R.id.iv_tab2);
iv_tab3 = (ImageView) findViewById(R.id.iv_tab3);
tv_tab0 = (TextView) findViewById(R.id.tv_tab0);
tv_tab1 = (TextView) findViewById(R.id.tv_tab1);
tv_tab2 = (TextView) findViewById(R.id.tv_tab2);
tv_tab3 = (TextView) findViewById(R.id.tv_tab3);
ll_tab0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickBottomTab(0);
}
});
ll_tab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickBottomTab(1);
}
});
ll_tab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickBottomTab(2);
}
});
ll_tab3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickBottomTab(3);
}
});
clickBottomTab(0);
}
//==============================================================================================
//底部tab点击事件
private void clickBottomTab(int tab) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
clearTabAndFragment(transaction);
switch (tab) {
case 0:
changeTab(tv_tab0, iv_tab0);
if (mainFragment == null) {
mainFragment = new MainFragment();
transaction.add(R.id.fl_content, mainFragment, "main");
} else {
transaction.show(mainFragment);
}
break;
case 1:
changeTab(tv_tab1, iv_tab1);
if (fujinFragment == null) {
fujinFragment = new FujinFragment();
transaction.add(R.id.fl_content, fujinFragment, "fujin");
} else {
transaction.show(fujinFragment);
}
break;
case 2:
changeTab(tv_tab2, iv_tab2);
if (jiekeFragment == null) {
jiekeFragment = new JiekeFragment();
transaction.add(R.id.fl_content, jiekeFragment, "jieke");
} else {
transaction.show(jiekeFragment);
}
break;
case 3:
changeTab(tv_tab3, iv_tab3);
if (wodeFragment == null) {
wodeFragment = new WodeFragment();
transaction.add(R.id.fl_content, wodeFragment, "main");
} else {
transaction.show(wodeFragment);
}
break;
}
transaction.commitAllowingStateLoss();
}
//==============================================================================================
//重置tab和Fragment
private void clearTabAndFragment(FragmentTransaction transaction) {
iv_tab0.setImageResource(R.drawable.icon_float_window);
iv_tab1.setImageResource(R.drawable.icon_float_window);
iv_tab2.setImageResource(R.drawable.icon_float_window);
iv_tab3.setImageResource(R.drawable.icon_float_window);
tv_tab0.setTextColor(ColorUtil.parse("#999999"));
tv_tab1.setTextColor(ColorUtil.parse("#999999"));
tv_tab2.setTextColor(ColorUtil.parse("#999999"));
tv_tab3.setTextColor(ColorUtil.parse("#999999"));
if (mainFragment != null) {
transaction.hide(mainFragment);
}
if (fujinFragment != null) {
transaction.hide(fujinFragment);
}
if (jiekeFragment != null) {
transaction.hide(jiekeFragment);
}
if (wodeFragment != null) {
transaction.hide(wodeFragment);
}
}
//==============================================================================================
//改变tab状态
private void changeTab(TextView tv, ImageView iv) {
tv.setTextColor(ColorUtil.parse("#333333"));
iv.setImageResource(R.drawable.icon_float_window_light);
}
}