效果:
Android实现底部导航菜单的跳转--BottomNavigationView_第1张图片
项目结构:
Android实现底部导航菜单的跳转--BottomNavigationView_第2张图片
Android实现底部导航菜单的跳转--BottomNavigationView_第3张图片
Android实现底部导航菜单的跳转--BottomNavigationView_第4张图片
需要用到design依赖: 现在是:implementation ‘com.android.support:design:28.0.0’
代码:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>        

my_fragment.xml(其他两个类似不在累述)

<?xml version="1.0" encoding="utf-8"?>    

home_bottom_tab_color_selsctor.xml

<?xml version="1.0" encoding="utf-8"?>
        

home_bottom_tab_my_selector.xml

<?xml version="1.0" encoding="utf-8"?>        

MainActivity

import android.support.annotation.NonNull;import android.support.design.widget.BottomNavigationView;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.KeyEvent;import android.view.MenuItem;import android.widget.FrameLayout;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    private  BottomNavigationView bottomNavigationView;    private Fragment work_fragment;    private Fragment message_fragment;    private Fragment my_fragment;    private Fragment[] fragments;    private int lastfragment;//用于记录上个选择的Fragment    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initFragment();    }    private void initFragment() {        work_fragment = new Work_Fragment();        message_fragment = new Message_Fragment();        my_fragment = new My_Fragment();        fragments = new Fragment[]{message_fragment, work_fragment, my_fragment};        lastfragment = 0;        getSupportFragmentManager().beginTransaction().replace(R.id.frame_fragment, message_fragment).show(message_fragment).commit();        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);        bottomNavigationView.setOnNavigationItemSelectedListener(changeFragment);    }    private BottomNavigationView.OnNavigationItemSelectedListener changeFragment = new BottomNavigationView.OnNavigationItemSelectedListener() {        @Override        public boolean onNavigationItemSelected(@NonNull MenuItem item) {            switch (item.getItemId()) {                case R.id.action_message: {                    if (lastfragment != 0) {                        switchFragment(lastfragment, 0);                        lastfragment = 0;                    }                    return true;                }                case R.id.action_work: {                    if (lastfragment != 1) {                        switchFragment(lastfragment, 1);                        lastfragment = 1;                    }                    return true;                }                case R.id.action_my: {                    if (lastfragment != 2) {                        switchFragment(lastfragment, 2);                        lastfragment = 2;                    }                    return true;                }            }            return false;        }    };    //切换Fragment    private void switchFragment(int lastfragment, int index) {        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();        transaction.hide(fragments[lastfragment]);//隐藏上个Fragment        if (fragments[index].isAdded() == false) {            transaction.add(R.id.frame_fragment, fragments[index]);        }        transaction.show(fragments[index]).commitAllowingStateLoss();    } /*   public boolean onKeyUp(int keyCode, KeyEvent event) {        //在主界面,连击连两下退出程序        if(keyCode==KeyEvent.KEYCODE_BACK){            long secondTime=System.currentTimeMillis();            if(secondTime-firstTime>800){                Toast.makeText(Home_Activity.this,"再按一次返回键退出",Toast.LENGTH_SHORT).show();                firstTime=secondTime;                return true;            }else{                WelcomeActivity.activity.finish();                System.exit(0);            }        }        return super.onKeyUp(keyCode, event);    }*/}

My_fragment(其他两个类似不在累述)

import android.os.Bundle;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class My_Fragment extends Fragment {    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view=inflater.inflate(R.layout.my_fragment,null);        return view;    }}

全部代码:https://github.com/EternalMsc/BottomNavigationView

更多相关文章

  1. 关于android 调用系统图片浏览器并返回图片路径问题
  2. Android保存图片到本地
  3. 【Android自学笔记】Android获取手机和存储卡上的图片
  4. 【Android笔记】Activity涉及界面全屏的方法
  5. android中如何显示图片的一部分
  6. android > layout > background 背景图片重复
  7. 如何在EditText中设置固定图片——Android移动开发

随机推荐

  1. 操作系统-进程的初步实现
  2. Linux内核开发双机调试(KGDB)
  3. 佳能iC MF4712怎么使用按键【扫描到计算
  4. MongoDB Server对于Sort排序能够支持的最
  5. vim命令
  6. LINUX 学习之路 yum仓库
  7. 公有云上基于微服务架构 SAAS 产品研发实
  8. OpenStack新建云主机流
  9. 为什么要学编程为什么是Python
  10. mongod入门系列之实战