首先在项目的gradle文件中添加
//navigation
implementation'com.google.android.material:material:1.1.0'
implementation'androidx.navigation:navigation-fragment:2.2.2'
implementation'androidx.navigation:navigation-ui:2.2.2'
添加menu和navigation的资源文件
res/menu/bottom_menu.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
android:id="@+id/navigation_index"
android:icon="@drawable/ic_home_black_24dp"
android:title="首页" />
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="家" />
android:id="@+id/navigation_search"
android:icon="@drawable/ic_home_black_24dp"
android:title="搜索" />
</menu>
res/navigation/mobile_navigation.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/navigation_index">
android:id="@+id/navigation_index"
android:name="com.example.applicationp0.bilibili.IndexFragment"
android:label="fragment_index"
tools:layout="@layout/fragment_index" />
android:id="@+id/navigation_home"
android:name="com.example.applicationp0.bilibili.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
android:id="@+id/navigation_search"
android:name="com.example.applicationp0.bilibili.SearchFragment"
android:label="fragment_search"
tools:layout="@layout/fragment_search" />
</navigation>
上面的部分是公共的。是做navgation所必须的,需要注意的是navgation文件中的fragment的id需要和menu中item的id,保持与一致,也就是上文中的navgation-index等。而一般,我们有两种情况来实现
1.官方的实现方式
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--填充的fragment内容-->
<!-- 底部导航栏-->
<!-- 使用fragment的方式-->
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation"
/>
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/design_default_color_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
在MainActivity.java中配置导航栏的controller
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navView = findViewById(R.id.nav_view);
//实例化naV_controller
AppBarConfiguration appBarConfiguration =new AppBarConfiguration.Builder(navView.getMenu()).build();
//声明appBar
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
2.使用FrameLayout自己设置监听的方式
在activity_main.xml中将fragment节点替换成以下FrameLayout
android:id="@+id/main_page_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在MainActivity.java中实现自己的跳转逻辑
package com.example.applicationp0;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import android.view.MenuItem;
import com.example.applicationp0.bilibili.HomeFragment;
import com.example.applicationp0.bilibili.IndexFragment;
import com.example.applicationp0.bilibili.SearchFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
/**
* 相同的部分在于
* 都是使用BottomNavigationView+fragment的切换来实现的
*
* 两种导航栏实现的逻辑在于
* 对于在avtivity中使用fragment的官方实现方式来说:
* 相当于我们把其中点击跳转fragment的逻辑放在了navController。简化了开发
*
* 对于在activity中使用FrameLayout的手动实现方式来说,
* 相当于我们手动的实现了其中点击跳转的逻辑。将其中的fragment来替换FrameLayout
* 需要我们手动初始化fragment并且设置事件的监听(navView.setOnNavigationItemSelectedListener)。
*
*/
public class MainActivityextends AppCompatActivity {
private HomeFragmenthomeFragment;
private IndexFragmentindexFragment;
private SearchFragmentsearchFragment;
private BottomNavigationViewnavView;
FragmentManagerfragmentManager = getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navView = findViewById(R.id.nav_view);
// //实例化naV_controller
// AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(navView.getMenu()).build();
// //声明appBar
// NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
// NavigationUI.setupWithNavController(navView, navController);
initView();
initListener();
//默认选择首页
switchFragment(indexFragment);
}
//设置事件监听
private void initListener() {
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_index:
switchFragment(indexFragment);
break;
case R.id.navigation_home:
switchFragment(homeFragment);
break;
case R.id.navigation_search:
switchFragment(searchFragment);
break;
}
return true;
}
});
}
private void switchFragment(Fragment targetFragment) {
FragmentTransaction transaction =fragmentManager.beginTransaction();
transaction.replace(R.id.main_page_container, targetFragment);
transaction.commit();
}
//初始化视图
private void initView() {
//初始化fragment
homeFragment=new HomeFragment();
indexFragment=new IndexFragment();
searchFragment=new SearchFragment();
}
}
其中还需要三个fragment。每个fragment实现自己的逻辑就可以了。
例如:indexFragment
package com.example.applicationp0.bilibili;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.applicationp0.R;
public class IndexFragmentextends Fragment {
@Nullable
@Override
public ViewonCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_index,container,false);
}
}
本人也是一名android的初学菜鸡。欢迎技术交流:1515914947。