android底部导航进行跳转,Android实现底部导航菜单的跳转--BottomNavigationView

效果:

b5b97b7ae45dfffd4e4cedf440165c17.png

项目结构:

32d39125131231fbd38c163acaf09dba.png

fdbb237d0f6d10ca2d822d3d936e41b0.png

d2a2b1229a206c5e887649307081a7e4.png

需要用到design依赖: 现在是:implementation ‘com.android.support:design:28.0.0’

代码:

activity_main.xml

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

home_bottom_tab_color_selsctor.xml

home_bottom_tab_my_selector.xml

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值