package com.example.outlier.test;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.ArrayList;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
public class MainActivity extends AppCompatActivity {
//这个变量表明当前是否处于登陆状态
private static boolean stateLogged = false;
public static boolean getStateLogged() {
return stateLogged;
}
public static void setStateLogged(boolean i) {
stateLogged = i;
}
//定义Fragement对象
private Fragment fg1;
private Fragment fg2;
private Fragment fg3;
private Fragment fg4;
// 定义FragmentManager对象管理器
private FragmentManager fragmentManager;
private ArrayList<AHBottomNavigationItem> bottomNavigationItems = new ArrayList<>();
private Handler handler = new Handler();
// UI
private AHBottomNavigation bottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
fragmentManager = getSupportFragmentManager();
setChioceItem(0);
initUI();
action();
}
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
/**
* Init UI
*/
private void initUI() {
bottomNavigation = (AHBottomNavigation) findViewById(R.id.bottom_navigation);
AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.tab_1, R.drawable.buttonhome_06, R.color.color_tab_1);
AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.tab_2, R.drawable.buttonhome_08, R.color.color_tab_2);
AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.tab_3, R.drawable.buttonhome_11, R.color.color_tab_1);
AHBottomNavigationItem item4 = new AHBottomNavigationItem(getString(R.string.tab_4), ContextCompat.getDrawable(this, R.drawable.buttonhome_03), ContextCompat.getColor(this, R.color.color_tab_2));
bottomNavigationItems.add(item1);
bottomNavigationItems.add(item2);
bottomNavigationItems.add(item3);
bottomNavigationItems.add(item4);
bottomNavigation.addItems(bottomNavigationItems);
bottomNavigation.setTranslucentNavigationEnabled(true);
bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
@Override
public boolean onTabSelected(int position, boolean wasSelected) {
switch (position) {
case 0:
setChioceItem(0);
break;
case 1:
setChioceItem(1);
break;
case 2:
setChioceItem(2);
break;
case 3:
setChioceItem(3);
break;
default:
break;
}
return true;
}
});
}
public void action() {
bottomNavigation.setColored(true);
bottomNavigation.restoreBottomNavigation(true);
bottomNavigation.setSelectedBackgroundVisible(true);
AHBottomNavigation.TitleState state = AHBottomNavigation.TitleState.ALWAYS_SHOW;
bottomNavigation.setTitleState(state);
}
/**
* 设置点击选项卡的事件处理
*
* @param index 选项卡的标号:0, 1, 2, 3
*/
private void setChioceItem(int index) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
hideFragments(fragmentTransaction);
switch (index) {
case 0:
// firstImage.setImageResource(R.drawable.XXXX); 需要的话自行修改
// firstLayout.setBackgroundColor(gray);
// 如果fg1为空,则创建一个并添加到界面上
if (fg1 == null) {
fg1 = new FirstFragment();
Log.e("", "" + fg1);
fragmentTransaction.add(R.id.content, fg1);
} else {
// 如果不为空,则直接将它显示出来
fragmentTransaction.show(fg1);
}
break;
case 1:
if (fg2 == null) {
fg2 = new SecondPage();
fragmentTransaction.add(R.id.content, fg2);
} else {
fragmentTransaction.show(fg2);
}
break;
case 2:
// thirdImage.setImageResource(R.drawable.XXXX);
// thirdLayout.setBackgroundColor(gray);
if (fg3 == null) {
fg3 = new ThirdFragment();
fragmentTransaction.add(R.id.content, fg3);
} else {
fragmentTransaction.show(fg3);
}
break;
case 3:
// fourthImage.setImageResource(R.drawable.XXXX);
// fourthLayout.setBackgroundColor(gray);
if (fg4 == null) {
fg4 = new FourthFragment();
fragmentTransaction.add(R.id.content, fg4);
} else {
fragmentTransaction.show(fg4);
}
break;
}
fragmentTransaction.commit(); // 提交
}
/**
* 隐藏Fragment
*
* @param fragmentTransaction
*/
private void hideFragments(FragmentTransaction fragmentTransaction) {
if (fg1 != null) {
fragmentTransaction.hide(fg1);
}
if (fg3 != null) {
fragmentTransaction.hide(fg3);
}
if (fg2 != null) {
fragmentTransaction.hide(fg2);
}
if (fg4 != null) {
fragmentTransaction.hide(fg4);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android动态导航栏的代码实现
共28个文件
png:14个
xml:9个
java:5个
5星 · 超过95%的资源 需积分: 16 64 下载量 15 浏览量
2017-08-06
02:24:22
上传
评论
收藏 80KB ZIP 举报
温馨提示
http://blog.csdn.net/qq_34861102/article/details/76737673 这个是和代码配套的博客,大家可以查看以后决定是否下载,适合新手入门吧
资源推荐
资源详情
资源评论
收起资源包目录
main.zip (28个子文件)
main
res
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
drawable
buttonhome_03.png 687B
buttonhome_11.png 617B
buttonhome_06.png 538B
buttonhome_08.png 1007B
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
values
colors.xml 1KB
strings.xml 2KB
styles.xml 383B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
layout
fg_1.xml 407B
fg_4.xml 482B
activity_main.xml 2KB
fg_3.xml 408B
fg_2.xml 408B
java
com
example
outlier
test
MainActivity.java 6KB
FirstFragment.java 533B
FourthFragment.java 517B
ThirdFragment.java 516B
SecondPage.java 513B
AndroidManifest.xml 718B
共 28 条
- 1
资源评论
- tergun2017-11-20挺好的啦啦
- A3961044592017-12-06正在学习安卓 准备跟着做一个demo
zz_Outlier
- 粉丝: 318
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功