当前位置:网站首页>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);
边栏推荐
- How does Jerry test the wrong touch rate of keys [chapter]
- 海思 VI接入视频流程
- Mask R-CNN
- Uniapp wechat login returns user name and Avatar
- Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
- Xshell configuration xforward forwarding Firefox browser
- [羊城杯2020]easyphp
- Jerry's built-in shutdown current is 1.2ua, and then it can't be turned on by long pressing [chapter]
- 2016. maximum difference between incremental elements
- 20220527_数据库过程_语句留档
猜你喜欢

数据标注典型案例,景联文科技如何助力企业搭建数据方案

容器化技术在嵌入式领域的应用

从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试

P1007 single log bridge

Lambda expression: an article takes you through

QT qpprogressbar details

LeetCode 968. Monitor binary tree

Odoo13 build a hospital HRP environment (detailed steps)

【硬件】标准阻值的由来

实现BottomNavigationView和Navigation联动
随机推荐
Data analysis learning records -- complete a simple one-way ANOVA with Excel
Freshman learning sharing
Boot actuator - Prometheus use
Splunk audit 的设定
20220527_数据库过程_语句留档
Jielizhi, production line assembly link [chapter]
psnr,ssim,rmse三个指标的定量分析
P7072 [csp-j2020] live broadcast Award
STM32串口DAM接收253字节就死机原因排查
【硬件】标准阻值的由来
PotPlayer设置最小化的快捷键
密码技术---分组密码的模式
海思3559万能平台搭建:在截获的YUV图像上旋转操作
Qt QProgressBar详解
设置单击右键可以选择用VS Code打开文件
Redis 过期策略+conf 记录
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - packaging based on thinkphp5 - detailed guidance of the author - July 1, 2022 08:43:
MySQL查询附近的数据.并按距离进行排序.
实现BottomNavigationView和Navigation联动
Uniapp wechat login returns user name and Avatar