Let’s say we need to implement a bottom navigation menu which will be controlled by API. This means that we can control from the serverside the following:

假设我们需要实现一个底部导航菜单,该菜单将由API控制 这意味着我们可以从服务器端控制以下内容:

  • item image项目图片
  • item label物品标签
  • items order物品订购
  • items count (this should be between 3 and 5 as BotttomNavigationView requirements)项目计数(根据BotttomNavigationView的要求,该数字应介于3到5之间)

First of all, we need to add to module level build.gradle file all the dependencies that we will use for this project.

首先,我们需要将用于该项目的所有依赖项添加到模块级别的build.gradle文件中。

Androidx Navigation

Androidx导航

def nav_version = "2.3.0"implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

Picasso and Rx java. We will use this to load menu items icons.

毕加索和Rx Java 。 我们将使用它来加载菜单项图标。

implementation "com.squareup.picasso:picasso:2.71828"implementation "io.reactivex.rxjava2:rxandroid:2.1.1"

Next, in our activity layout file we should add the BottomNavigationView, which we will populate later, and a NavHostFragment that will host all our fragments that can be triggerd by menu items

接下来,在活动布局文件中,我们应添加BottomNavigationView(稍后将填充)和NavHostFragment,该主机将容纳所有可通过菜单项触发的片段

<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        android:id="@+id/nav_host_fragment"        android:name="androidx.navigation.fragment.NavHostFragment"        android:layout_width="0dp"        android:layout_height="0dp"        app:defaultNavHost="true"        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/main_nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView        android:id="@+id/nav_view"        android:layout_width="match_parent"        android:layout_height="64dp"        android:background="@color/colorPrimary"        app:itemIconTint="@color/menu_item"        app:itemTextColor="@color/menu_item"        app:labelVisibilityMode="labeled"        app:layout_constraintBottom_toBottomOf="parent"        tools:visibility="visible" />

</androidx.constraintlayout.widget.ConstraintLayout>

As you can see, NavHostFragment has an attribute called navGraph. To create this file, first we should create the Fragments that will represent the items that we can receive from the API. In this example we will use 3 fragments:

如您所见,NavHostFragment具有一个称为navGraph的属性。 要创建此文件,首先我们应该创建片段,这些片段代表可以从API接收的项目。 在此示例中,我们将使用3个片段:

  • Home

  • Chat

    聊天室

  • Profile

    个人资料

After creating those fragments, we come back to navGraph. In res/navigation directory, create a navigation resource file called main_nav_graph. It should look like this:

创建完这些片段后,我们回到navGraph。 在res/navigation目录中,创建一个名为main_nav_graph.的导航资源文件main_nav_graph. 它看起来应该像这样:

<?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"    android:id="@+id/main_nav_graph"    app:startDestination="@id/home_dest">

    <fragment        android:id="@+id/home_dest"        android:name="com.comandenis.example.home.HomeFragment" />    <fragment        android:id="@+id/chat_dest"        android:name="com.comandenis.example.chat.ChatFragment" />    <fragment       android:id="@+id/profile_dest"       android:name="com.comandenis.example.profile.ProfileFragment" /></navigation>

Here should be declared a destination for all the fragments that is planned to have on bottom navigation view. Don’t forget to add startDestination property!

应该将此处声明为计划在底部导航视图上包含的所有片段的目的地。 不要忘记添加startDestination属性!

Let’s assume that we have all the data from the API and will map them into a MenuItem data class which will have the properties that we want to use in the menu. For this example we will use a labeled menu so we will need this 3 properties:

假设我们拥有API中的所有数据,并将它们映射到MenuItem数据类中,该类具有要在菜单中使用的属性。 在此示例中,我们将使用带标签的菜单,因此我们需要以下3个属性:

data class MenuItem(    val label: String,    val image: String,    val destinationId: Int)

For the purpose of this post will create a hard-coded list of menu items

出于这篇文章的目的,将创建菜单项的硬编码列表

private val menuItems = listOf(    MenuItem(        label = "Home",         image = "https://i.ibb.co/B4nR76t/home-24px.png",         destinationId = R.id.home_dest),    MenuItem(        label = "Chat",         image = "https://i.ibb.co/6gG1M71/message-24px.png",         destinationId = R.id.chat_dest),    MenuItem(        label = "Profile",         image = "https://i.ibb.co/FHP56wG/perm-identity-24px.png",         destinationId = R.id.profile_dest))

destinationId is the id of the fragment declared in the nav graph, so after getting the data from the API we should map that list so we can add the destinationId for each item.

destinationId是在导航图中声明的片段的ID,因此从API获取数据后,我们应该映射该列表,以便为每个项目添加destinationId。

Finally, let’s add the items to bottom navigation

最后,让我们将项目添加到底部导航中

menuItems.forEachIndexed { index, menuItem ->nav_view.menu.add(Menu.NONE, menuItem.destinationId, index, menuItem.label)}

Iterate over the menuItems list and add each item to the menu.

遍历menuItems列表,并将每个项目添加到菜单。

After doing this the result will be something like this.

完成此操作后,结果将是这样。

It’s obvious that we still need to add icons for this items, so let’s do it!

显然,我们仍然需要为此项目添加图标,所以让我们开始吧!

data class Tuple(val menuItem: MenuItem, val bitmap: Bitmap)

val picasso = Picasso.get()

menuItems.forEachIndexed { index, menuItem ->nav_view.menu.add(Menu.NONE, menuItem.destinationId, index, menuItem.label)}subscriptions.add(    Observable.fromIterable(menuItems)        .switchMap {Observable.just(                Tuple(it, picasso.load(it.image).get())            )        }.subscribeOn(Schedulers.io())        .observeOn(AndroidSchedulers.mainThread())        .subscribe(            {val menuItem = nav_view.menu.findItem(it.menuItem.destinationId)                menuItem.icon = BitmapDrawable(resources, it.bitmap)            },            {// Handle errors here            },            {// On complete we should setup nav controller                val navController = findNavController(R.id.nav_host_fragment)

                nav_view.setupWithNavController(navController)            }))

Using rx java, I created an observable from the menu items list that emits each item. For each of those items we will use Picasso to get the from URL, then we will emit further a Tuple with the item and the bitmap that we get from Picasso.

使用rx java,我从发出每个项目的菜单项目列表中创建了一个可观察对象。 对于这些项目中的每一个,我们将使用Picasso从URL中获取,然后我们将进一步发送一个Tuple,其中包含该项目以及从Picasso中获得的位图。

When item arrive on the onNext method will search for the item in the menu that is already populated, convert the bitmap into drawable and set the icon of the item with it.

当项目到达时, onNext方法将在已填充的菜单中搜索该项目,将位图转换为可绘制的并设置项目的图标。

Here is the result

这是结果

Hope this will help you. Feel free to get an hands-on and ask anything.

希望这会帮助你。 随时动手做任何事情。

Happy Coding!

编码愉快!

翻译自: https://medium.com/agile-freaks/android-dynamic-bottom-navigation-e6818942e27a


http://www.taodudu.cc/news/show-2845723.html

相关文章:

  • 悬浮导航
  • VIM文件导航
  • php序顶部导航,页面上下滚动改变顶部导航的定位方式
  • 科目二-倒车入库
  • 科目二需要注意的点(笔记包含图片讲解)
  • java科目二踩线原理,右倒车入库总踩线?是因为没有看到这些“秘诀”
  • 模拟科目二倒车入库训练
  • 科目二 车速忽快忽慢
  • 计算机ms高级应用科目一 科目二考什么,什么是科目一、科目二、科目三、科目四?全部都在这!...
  • 科目二考试之倒车入库步骤
  • 科目二 点位
  • 科目二难点——倒车入库
  • 驾考科目二注意事项
  • oracle 倒库详细步骤,超详细的倒车入库步骤
  • oracle 倒库详细步骤,科目二倒车入库步骤详解,考前必看!
  • 电容传感器
  • numpy pandas matplotlib 学习笔记
  • 柔性电子: Triboelectric Nanogenerator摩擦生电
  • 中国生物降解塑料行业“十四五”发展规划及未来前景展望报告2021年版
  • Combining Visual Cues with Interactions for 3D–2D Registration in Liver Laparoscopy翻译
  • 空间连续体上接触力的无线传感和定位
  • 读论文,第十一天:Flexible Strain Sensors for Wearable Hand Gesture Recognition: From Devices to Systems
  • 中国生物降解塑料行业市场运营态势及发展趋势研究报告2022~2028年
  • 【Stanford Online】Engineering: Algorithms1 NO.2 Asymptotic analysis
  • 柔性电子,常用材料总结
  • 上海交大和MIT提出的软性机械手,可提供实时的触觉控制
  • 洛谷 P1008
  • P1008 [NOIP1998 普及组] 三连击
  • 【Python】P1008 [NOIP1998 普及组] 三连击
  • 洛谷学习笔记P1008

Android动态底部导航相关推荐

  1. 仿淘宝Android实现底部导航栏图标溢出效果-clipChildren属性

    1.clipChildren和clipPadding说明 clipChildren用来定义他的子控件是否要在他应有的边界内进行绘制. 默认情况下,clipChild被设置为true. 也就是不允许进行 ...

  2. android 固定底部导航,如何设置android底部导航栏位置固定在android

    请帮我设置底部导航栏位置固定在底部, ,因为我在输入editText字段时遇到问题,底部导航栏向上移动并覆盖其他领域如何设置android底部导航栏位置固定在android 代码: xmlns:and ...

  3. Android应用底部导航栏(选项卡)实例

    现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能. 我们先看下该demo实例的框架图: 其 ...

  4. android底部导航栏软件,三步搞定android应用底部导航栏

    很多android应用底部都有一个底部导航栏,方便用户在使用过程中随意切换.目前常用的做法有三种:一种是使用自定义tabHost,一种是使用activityGroup,一种是结合FrameLayout ...

  5. 三步搞定android应用底部导航栏

    很多android应用底部都有一个底部导航栏,方便用户在使用过程中随意切换.目前常用的做法有三种:一种是使用自定义tabHost,一种是使用activityGroup,一种是结合FrameLayout ...

  6. android radiobutton底部导航,android中Fragment+RadioButton实现底部导航栏

    在App中经常看到这样的tab底部导航栏 那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activity ...

  7. Android BottomNavigationBar底部导航控制器的使用(包含默认postion的设置)

    转载请标明出处:http://blog.csdn.net/u010046908/article/details/50962081本文出自:[李东的博客] 最近Google在自己推出的Material ...

  8. 【Android】底部导航栏【BottomNavigationView】+【ViewPage2】

    问题需求 实现底部导航栏切换 问题解决 最简单的实现方式就是使用系统自动生成的模板页面,但是有时候会有一些问题,特别是需要去除[ActionBar]的情况下,这种情况下使用系统的模板页面就不好用了,此 ...

  9. Android 自定义底部导航栏消息显示

    转载请标明出处: http://blog.csdn.net/tyzlmjj/article/details/47186249 本文出自:[M家杰的博客] 概述 底部导航栏的做法多种多样,这次主要用到了 ...

最新文章

  1. android炫酷的自定义view,Android自定义View实现炫酷进度条
  2. 力扣(LeetCode)刷题,简单题(第27期)
  3. 基于TensorFolw的人工智能影像诊断平台工作原理解析
  4. 测试机型不够?把玩家反馈过的问题机型,都测个遍
  5. 在Object-C中学习数据结构与算法之排序算法
  6. HttpUtility.UrlEncode 方法 (String) 对 URL 字符串进行编码 NET Framework 4.6 and 4.5
  7. Android软件开发-ProgressBar
  8. java反编译之 基础篇(class未加密未混淆)
  9. 汇编语言的强制类型转换
  10. 英睿达固态硬盘测试软件,高速读写,电竞必备 英睿达P5固态硬盘评测
  11. DOSBOX常用快捷键DEBUG指令
  12. 机器学习推荐算法之关联规则(Apriori)——支持度;置信度;提升度
  13. Java基于ssm开发的古董竞标拍卖系统也可以改成拍卖品网站
  14. 2017湖湘杯Writeup
  15. Linux-Udev机制
  16. java utf8 简繁转换 类库,java 中文繁简体转换工具 opencc4j
  17. 【详解】机器学习库-Matplotlib+Numpy+Pandas
  18. 关于微信小程序获取头像和昵称
  19. spring项目一启动就自动运行方法(资源加载初始化)
  20. 【domain gap 含义】

热门文章

  1. 破圈的《张朝阳的物理课》,开启“知识突围”的搜狐视频
  2. 临床执业助理医师(综合练习)题库【4】
  3. 词向量表示:word2vec与词嵌入
  4. 【数据分析师自学系列-MySQL】创建新表create table、create table as、create table like的区别
  5. MySQL结课体会,听课心得体会小结
  6. 二手交易平台/二手交易系统/闲置物品交易系统
  7. GET和POST两种基本请求方法的区别 1
  8. 基于HTML美中华传统文化题材网页项目的设计与实现 (纯HTML+CSS制作中国茶文化网站)
  9. USB 发展 缺陷 与 未来
  10. 第三方移动支付类产品竞品分析:支付宝VS微信支付VS云闪付