当前位置:网站首页>Realize the linkage between bottomnavigationview and navigation
Realize the linkage between bottomnavigationview and navigation
2022-07-02 23:07:00 【Automatic 2004 haojinhui】
Realization BottomNavigationView and Navigation linkage
Interface display

Introduce dependencies
// Navigation
implementation "androidx.navigation:navigation-fragment:2.2.2"
implementation "androidx.navigation:navigation-ui:2.2.2"
establish Fragment


establish navigation

<?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/navigation" app:startDestination="@id/oneFragment2">
<fragment android:id="@+id/oneFragment2" android:name="com.example.navigation.OneFragment" android:label="fragment_one" tools:layout="@layout/fragment_one" />
<fragment android:id="@+id/twoFragment2" android:name="com.example.navigation.TwoFragment" android:label="fragment_two" tools:layout="@layout/fragment_two" />
<fragment android:id="@+id/threeFragment2" android:name="com.example.navigation.ThreeFragment" android:label="fragment_three" tools:layout="@layout/fragment_three" />
</navigation>
Notice the three here ID
by BottomNavigationView establish menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/oneFragment2" android:icon="@drawable/ic_launcher_background" android:title="Item" />
<item android:id="@+id/twoFragment2" android:icon="@drawable/ic_launcher_background" android:title="Item" />
<item android:id="@+id/threeFragment2" android:icon="@drawable/ic_launcher_background" android:title="Item" />
</menu>
Three here ID It must be the same as just now 
Main interface layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" >
<androidx.fragment.app.FragmentContainerView android:id="@+id/fragmentContainerView" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/main_bottom_navigation_view" android:layout_width="match_parent" android:layout_height="50dp" app:menu="@menu/menu" />
</LinearLayout>
Realization BottomNavigationView and Navigation linkage

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.tabs.TabLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragmentContainerView);
NavController navController = navHostFragment.getNavController();
BottomNavigationView bottomNavigationView = findViewById(R.id.main_bottom_navigation_view);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
}
Mainly three sides and four lines of code
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragmentContainerView);
NavController navController = navHostFragment.getNavController();
BottomNavigationView bottomNavigationView = findViewById(R.id.main_bottom_navigation_view);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
边栏推荐
- To myself who is about to work
- [favorite poems] OK, song
- SQL进阶语法
- Loss function~
- [Solved] Splunk: Cannot get username when all users are selected“
- Hanging mirror security won four global infosec awards on rsac2022
- Value sequence < detailed explanation of daily question >
- Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
- Golang's learning route
- [leetcode] reverse string [344]
猜你喜欢

Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
![[NPUCTF2020]ezlogin xPATH注入](/img/6e/dac4dfa0970829775084bada740542.png)
[NPUCTF2020]ezlogin xPATH注入

Introduction to the latest plan of horizon in April 2022

ServletContext learning diary 1

Application of containerization technology in embedded field

數據分析學習記錄--用EXCEL完成簡單的單因素方差分析

地平线2022年4月最新方案介绍

Hanging mirror security won four global infosec awards on rsac2022

Splunk audit 的设定

MySQL queries nearby data And sort by distance
随机推荐
Performance optimization - rigorous mode
antd组件upload上传xlsx文件,并读取文件内容
PMP project integration management
深度剖析数据在内存中的存储----C语言篇
静态文件显示问题
海思3559万能平台搭建:在截获的YUV图像上旋转操作
数据分析学习记录(二)---响应曲面法及Design-Expert的简单使用
psnr,ssim,rmse三个指标的定量分析
WebRTC音视频采集和播放示例及MediaStream媒体流解析
用matlab调用vs2015来编译vs工程
Brief introduction of emotional dialogue recognition and generation
Easyclick, EC Quanlang network verification source code
xshell配置xforward转发火狐浏览器
Jerry's built-in short press and long press, no matter how long it is, it is a short press [chapter]
实现BottomNavigationView和Navigation联动
STM32之ADC
Freshman learning sharing
Loss function~
Distributed monitoring system ZABBIX
损失函数~