Android支持直接创建带有NavigationView的Activity,这里主要介绍NavigationView的逻辑。

NavigationView通常是跟DrawerLayout一起使用。DrawerLayout下面就包含俩个控件,第一个是内容,第二个是滑屏。

页面

页面代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<androidx.coordinatorlayout.widget.CoordinatorLayout
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=".module.MainActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.frist.AppBarOverlay">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/bgbule"
app:popupTheme="@style/Theme.frist.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<fragment
android:id="@+id/nav_host_fragment_content_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_hostfragment_main" />


<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/nav_menu_main" />
</androidx.drawerlayout.widget.DrawerLayout>

容器1

如代码所示,内容里放了一个可以被控制的fragment,name为androidx.navigation.fragment.NavHostFragment代表他是被NavigationView控制的fragment。

app:navGraph属性,设置为nav_hostfragment_main,代表控制他的xml文件是navigation文件夹下的nav_hostfragment_main。

nav_hostfragment_main.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/nav_hostfragment_main"
app:startDestination="@+id/nav_frist">

<fragment
android:id="@+id/nav_frist"
android:name="com.kiba.learn.module.Fragment.FristFragment"
android:label="first"
tools:layout="@layout/fragment_frist" /> </navigation>

app:layout_behavior属性为固定字符串【com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior】,代表他是在头部的bar之下。

容器2

容器2是NavigationView,app:headerLayout指定了NavigationView的Header的Xml文件;app:menu指定了NavigationView菜单的Xml文件。

菜单的xml很简单,就是一组item,如下:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
<item
android:id="@+id/nav_frist"
android:icon="@drawable/ic_menu_gallery"
android:title="frist" /> </group>
</menu>

代码

Activity代码如下:

通过NavController指定页面内的fragment为可以被控制的fragment,然后实现,滑出的菜单点击,切换fragment。

private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);//设置自定义bar //region navView配置
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_first)
.setDrawerLayout(binding.drawerLayout)
.build();//配置切换fragment时,使用的id
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);//指定页面内的fragment为可以被控制的fragment
//绑定fragment和navigation的配置
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
binding.navView.setCheckedItem(R.id.nav_frist);//设置当前选中项
binding.navView.setNavigationItemSelectedListener(view->{
int s= view.getItemId();

return true;
});
//endregion

Fragment currentFragment = FragmentManager.findFragment(this.findViewById(R.id.nav_host_fragment_content_main));//获取当前Activity下的fragment }

//显示并配置右上设置菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.set_menu, menu);
return true;
}

//显示并配置左上navigation控制按钮
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
//系统返回按钮触发
@Override
public void onBackPressed() {
fristFragment sf = (fristFragment)getFragment(fristFragment.class);
Intent loginActivity=new Intent(this,LoginActivity.class);
startActivity(loginActivity);
super.onBackPressed();
}

public Fragment getFragment(Class<?> clazz) {
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments!= null && fragments.size() > 0) {
NavHostFragment navHostFragment = (NavHostFragment) fragments.get(0);
List<Fragment> childfragments = navHostFragment.getChildFragmentManager().getFragments();
if(childfragments != null && childfragments.size() > 0){
for (int j = 0; j < childfragments.size(); j++) {
Fragment fragment = childfragments.get(j);
if(fragment.getClass().isAssignableFrom(clazz)){

return fragment;
}
}
}
}
return null;
}

PS1:使用FragmentManager可以获取到当前Activity下 的fragment 如:FragmentManager.findFragment(this.findViewById(R.id.xxx));

PS2:onCreateOptionsMenu的重新会让右上角的菜单显示,不重载则不显示。

PS3:onSupportNavigateUp会让左上角的菜单显示,不重载则不显示。

PS4:使用NavigationView导航fragment时,fragment的name是androidx.navigation.fragment.NavHostFragment,此时想获取指定的fragment可以使用getFragment函数,注意该函数在onCreate中获取不到fragment。

调用代码如下:

SFragment sf = (SFragment)getFragment(SurveyFragment.class);

  

----------------------------------------------------------------------------------------------------

注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的【推荐】,非常感谢!

NavigationView使用简介的更多相关文章

  1. NavigationViewDemo【和DrawerLayout搭配使用实现侧滑导航视图界面】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 主要记录NavigationView的使用,而一般情况下NavigationView是和DrawerLayout搭配使用的,还有To ...

  2. 一个C#开发搭建Android框架的心路历程

    前言 Java框架实在是太多了,因为是初学乍练,所以,只好以百度为标准选择框架了. Java的框架文章太难写了,因为他引用了太多框架,而没一个框架都有很繁琐的配置,把每个框架都写一遍,就等于写书了:所 ...

  3. 【转】GitHub 排名前 100 的安卓、iOS项目简介

    GitHub Android Libraries Top 100 简介 排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不 ...

  4. GitHub Android Libraries Top 100 简介

    本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过 ...

  5. 2016年GitHub 排名前 100 的安卓、iOS项目简介(收藏)

    排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不相关的项目, 所以排名并不具备任何官方效力, 仅供参考学习, 方便初学者 ...

  6. 64.GitHub 排名前100的android项目简介

    GitHub Android Libraries Top 100 简介 排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不 ...

  7. GitHub Android Librarys Top 100 简介

    GitHub Android Librarys Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据GitHub搜索J ...

  8. <Android开源库 ~ 1> GitHub Android Libraries Top 100 简介

    转载自GitHub Android Libraries Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitH ...

  9. Material Design: NavigationView FlaotingActionBar SnackBar采用

    转载 请明确说明 MingsangAndroid 本文介绍了Design Support Library的引入 拥抱Android Design Support Library新变化(导航视图.悬浮A ...

随机推荐

  1. JavaScrip中 Array.reduce()

    数组的方法 reduce() reduce方法在数组的每一项元素上都会执行回调函数. 语法:array.reduce( callBack [ , init]  ) // 语法arrary.reduce ...

  2. 358 day09字节流、字符流

    day09[字节流.字符流] 主要内容 IO流 字节流 字符流 异常处理 Properties 教学目标 [ ] 能够说出IO流的分类和功能 [ ] 能够使用字节输出流写出数据到文件 [ ] 能够使用 ...

  3. 3gcms导航,实现当前栏目高亮的办法

    <volist name="menu" id="vo" offset="0" length='8' key='k'> <l ...

  4. ecshop调用指定栏目下的文章的方法

    打开 index.php 添加 fun函数一个,需放在<php与?>中间. /** * 获得指定栏目的文章列表. * @param int $cid 栏目ID * @param int $ ...

  5. xmind使用技巧

    xmind看似每个人都会使用,但是掌握一些小技巧,能够有效提升工作效率. 多行复制粘贴 在xmind中选中多行,复制然后可以直接粘贴到excel.word当中. 在excel.word选中多行,复制然 ...

  6. php无限分类 构建树形结构

    <?php class Classification { const PARENT_ID = 'parentid'; const ID = 'id'; const CHILDREN = 'chi ...

  7. Appium自动化测试时为什么要自己封装find方法

    官方的find_element方法不能很好地处理异常,所以自行封装,以智能化处理各种异常

  8. CF573D-Bear and Cavalry【动态dp】

    正题 题目链接:https://www.luogu.com.cn/problem/CF573D 题目大意 给出\(n\)个人\(n\)匹马,每个人/马有能力值\(w_i\)/\(h_i\). 第\(i ...

  9. 《集体智慧编程学习笔记》——Chapter2:提供推荐

    知识点: 1. 协作型过滤--Collaboraive Filtering 通常的做法是对一群人进行搜索,并从中找出与我们品味相近的一小群人,算法会对这些人的偏好进行考察,并将它们组合起来构造出一个经 ...

  10. Vue2.0 基础入门

    前言:" 今生遇汝,何其幸哉:于我蒙昧之时遇到你,于我大雾初透之时爱上你,于我大智初醒之时沉沦你. " 官网: 介绍 - Vue.js (vuejs.org) 指令与修饰符 创建实 ...