在学习底部导航栏时,使用了AndroidStudio中快速创建方式创建了一个带有底部导航栏的Activity。
先贴上全部的代码,后边会分开片段进行标注:
package com.example.my_project;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
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.support.annotation.NonNull;
import android.view.MenuItem;
import index_fragment.Gundam;
import index_fragment.Personal;
import index_fragment.The_star_Trek;
import static com.example.my_project.R.id.fl_container;
public class MainActivity extends AppCompatActivity {
private Fragment fragment1,fragment2,fragment3;
private Fragment[] fragments;
private int lastfragment;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
if (lastfragment != 0) {
switchFragment(lastfragment, 0);
lastfragment = 0;
}
return true;
case R.id.navigation_dashboard:
if (lastfragment != 1) {
switchFragment(lastfragment, 1);
lastfragment = 1;
}
return true;
case R.id.navigation_notifications:
if (lastfragment != 2) {
switchFragment(lastfragment, 2);
lastfragment = 2;
}
return true;
}
return false;
}
};
private void switchFragment(int lastfragment, int index) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.hide(fragments[lastfragment]);//隐藏上个Fragment
if (fragments[index].isAdded() == false) {
transaction.add(fl_container, fragments[index]);
}
transaction.show(fragments[index]).commitAllowingStateLoss();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
initFragment();
}
private void initFragment(){
fragment1=new Gundam();
fragment2=new The_star_Trek();
fragment3=new Personal();
fragments=new Fragment[]{fragment1,fragment2,fragment3};
lastfragment=0;
getSupportFragmentManager().beginTransaction().replace(fl_container,fragment1).show(fragment1).commitAllowingStateLoss();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
}
底部导航栏我创建了三个,因此需要关联三个Fragment,所有首先声明三个Fragment,绑定具体的fragment对象
声明Fragment数组,用来存放三个fragment对象。
int类型的lastfragment存放点击事件前的fragment是具体的第几个
在onNavigationItemSelected中,通过switch语句判断被点击的按钮是第几个,判断item的id来进行分发操作
在case中调用switchFragment方法,传入点击前lastfragment和点击的index值来进行替换操作
private Fragment fragment1,fragment2,fragment3;
private Fragment[] fragments;
private int lastfragment;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
if (lastfragment != 0) {
switchFragment(lastfragment, 0);
lastfragment = 0;
}
return true;
case R.id.navigation_dashboard:
if (lastfragment != 1) {
switchFragment(lastfragment, 1);
lastfragment = 1;
}
return true;
case R.id.navigation_notifications:
if (lastfragment != 2) {
switchFragment(lastfragment, 2);
lastfragment = 2;
}
return true;
}
return false;
}
};
在switchFragment中声明一个FragmentTransaction对象,用于替换Fragment并替换上新的Fragment
private void switchFragment(int lastfragment, int index) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.hide(fragments[lastfragment]);//隐藏上个Fragment
if (fragments[index].isAdded() == false) {
transaction.add(fl_container, fragments[index]);
}
transaction.show(fragments[index]).commitAllowingStateLoss();
}