使用fragment实现底部导航菜单栏

2023-05-16

  在实现这个功能的过程中,走了很多的弯路,也花费了较长的时间,作为一个新手,实现这个功能的过程中,分了几个步骤进行尝试,首先是会使用fragment,这个见另一篇博文:了解与使用fragment,链接:https://blog.csdn.net/lz050116/article/details/107191940,然后是了解hide()add()replace()show()等方法的使用,最后写相应的逻辑,实现底部导航菜单栏的切换。

1.编写fragment的布局文件

  我写了两个布局文件,分别为fragment_appsfragment_me

1.1布局文件—fragment_apps.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/fragment_apps"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activity.FragmentApps">

    <!-- TODO: Update blank fragment layout -->
    <!-- 辅助线 -->
    <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline_left"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintGuide_begin="24dp"/>
    <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline_right"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintGuide_end="24dp"/>

    <!-- 图标 产品管理 -->
    <com.christieli.automaintenancemanagement.view.IconView
            android:id="@+id/product_manager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/product"
            android:textSize="@dimen/text_size_icon_large"
            android:textColor="@color/poolblue"
            android:paddingTop="@dimen/margin_middle"
            app:layout_constraintLeft_toLeftOf="@id/guideline_left"
            tools:ignore="MissingConstraints"/>
    <TextView
            android:id="@+id/product_manager_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="产品管理"
            android:textSize="@dimen/text_size_min"
            android:textColor="@color/colorTextBlack"
            android:layout_marginTop="@dimen/margin_min"
            app:layout_constraintLeft_toLeftOf="@+id/product_manager"
            app:layout_constraintRight_toRightOf="@+id/product_manager"
            app:layout_constraintTop_toBottomOf="@+id/product_manager"
            tools:ignore="DuplicateIds"/>
</androidx.constraintlayout.widget.ConstraintLayout>

注:我的布局文件中一共有8个类似于product_manager的图标

1.2布局文件—fragment_me.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:id="@+id/fragment_me"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:gravity="top|center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activity.FragmentMe">

    <!-- 顶部基础信息栏 -->
    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/me_information"
            android:layout_width="match_parent"
            android:layout_height="96dp"
            android:background="@color/colorPrimary"
            tools:ignore="MissingConstraints">
        <ImageView
                android:id="@+id/me_image"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="@color/line"
                app:layout_constraintLeft_toLeftOf="@+id/me_information"
                app:layout_constraintTop_toTopOf="@+id/me_information"
                android:layout_marginLeft="24dp"
                android:layout_marginTop="24dp"
                android:contentDescription="@string/arrows_right_icon"/>
</LinearLayout>

注:还有一些其他的组件,不一一列出

1.2布局文件—activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <include layout="@layout/title_bar"/>
    <include layout="@layout/tool_bar"/>
    <fragment

              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintTop_toBottomOf="@+id/title_bar"
              android:name="com.christieli.automaintenancemanagement.activity.FragmentApps"
              tools:ignore="MissingConstraints"/>
    <FrameLayout
              android:id="@+id/fragment_container"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintTop_toBottomOf="@+id/title_bar"
              tools:ignore="MissingConstraints"/>
</androidx.constraintlayout.widget.ConstraintLayout>

2.编写FragmentApps.java和FragmentMe.java

  两个文件是一致的,故我只列出其中一个:

import android.os.Bundle;
        import androidx.fragment.app.Fragment;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
       import com.christieli.automaintenancemanagement.R;
       
public class FragmentApps extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_apps, container, false);
    }
}

3.更改MainActivity.java

  MainActivity.java代码如下:

import android.view.View;
import android.os.Bundle;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.christieli.automaintenancemanagement.activity.FragmentApps;
import com.christieli.automaintenancemanagement.activity.FragmentMe;
import com.christieli.automaintenancemanagement.view.IconView;


public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private Fragment mFragmentApps;
    private Fragment mFragmentMe;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFragmentApps = new FragmentApps();
        // getSupportFragmentManager():获取Fragment的管理对象
        // beginTransaction():开始一个事务
        // add():加入一个事务,将fragment加入到对应的位置
        // commit():提交
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, mFragmentApps).commit();

        IconView appsIcon = findViewById(R.id.apps_icon);
        TextView appsText = findViewById(R.id.apps_text);
        IconView meIcon = findViewById(R.id.me_icon);
        TextView meText = findViewById(R.id.me_text);
        appsIcon.setOnClickListener(this);
        appsText.setOnClickListener(this);
        meIcon.setOnClickListener(this);
        meText.setOnClickListener(this);
    }
    
    public void onClick(View v) {
        // 隐藏所有Fragment
        hideAllFragment();
        switch (v.getId()) {
            // 点击图标显示对应的Fragment
            case R.id.apps_icon:
            case R.id.apps_text:
                if (mFragmentApps == null) {
                    mFragmentApps = new FragmentApps();
                    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, mFragmentApps).commit();
                } else {
                    getSupportFragmentManager().beginTransaction().show(mFragmentApps).commit();
                }
                break;
            case R.id.me_icon:
            case R.id.me_text:
                if (mFragmentMe == null) {
                    mFragmentMe = new FragmentMe();
                    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, mFragmentMe).commit();
                } else {
                    getSupportFragmentManager().beginTransaction().show(mFragmentMe).commit();
                }
                break;
            default:
                break;
        }
    }

    // 隐藏所有Fragment
    private void hideAllFragment() {
        if (mFragmentApps != null) {
            // hide():隐藏fragment
            getSupportFragmentManager().beginTransaction().hide(mFragmentApps).commit();
        }
        if (mFragmentMe != null) {
            getSupportFragmentManager().beginTransaction().hide(mFragmentMe).commit();
        }
    }
}

4.实现效果

在这里插入图片描述

5.注意事项

5.1 fragment != null!fragment.isHiden()的判断区别

  要注意在判断是否隐藏fragment时,使用上述的哪个判断条件,若使用!fragment.isHiden(),要确认前面已经new了fragment对象,否则会出现异常,建议此时直接使用fragment != null

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用fragment实现底部导航菜单栏 的相关文章

  • Android Studio模拟器启动后不停闪烁(未解决)

    问题描述 xff1a Android Studio模拟器启动后不停闪烁 解决方法 xff1a 右侧点击Device Manager打开设备管理 xff0c 点击修改标志 将Graphics 图样 换成Software 软件 xff0c 点击
  • 复杂网络建模的实现(哈工大深圳复杂网络建模课程Project)

    任务 xff1a 1 xff0c 三张不同的网络 xff1a 已知某人的名称 已知某人的家乡 已知某人的方言 分析这三者各自的网络性能 xff08 节点度数分布 平均最短路径长度 集聚系数 xff09 以及动态行为 xff08 在刻意攻击
  • 免登陆Oracle官网下载JDK

    Oracle官网下载JDK 方法一 免登录下载 进入官网 xff0c 选择需要下载的JDK版本 xff0c 这里以JDK8为例 点击下载 xff0c 勾选同意 在正常情况下 xff0c 点击 Download jdk 8u333 windo
  • linux rancher 清理docker容器磁盘空间

    目录说明 var lib docker containers xff1a 是 Docker 在 Linux 系统上默认存储容器信息的目录 在该目录下 xff0c 每个运行的 Docker 容器都有一个单独的目录 xff0c 以容器 ID 命
  • 如何在官网下载COCO数据集

    官网地址 xff1a https cocodataset org download 1 选择下载的数据 xff0c 右键 xff0c 获取下载地址 2 将 http 改为 https 示例获得的下载地址为 xff1a http images
  • 两个互相引用对象的垃圾回收

    部分转自 xff1a 深入理解java虚拟机 一书 判断对象是否存活 1 引用计数算法 给对象添加一个引用计数器 xff0c 每当有一个地方引用它时 xff0c 计数器值就加1 当引用失效时 xff0c 计数器值就减1 任何时刻计数器为0的
  • ssm整合时,通过jdbc.properties文件无法连接mysql问题

    最近在重温ssm框架 在搭建基础的项目进行单元测试时 xff0c 发现无法连接mysql数据库 通过各种查资料终于发现了原因 原始jdbc properties文件 由于username这个属性会被系统的username变量覆盖 xff0c
  • Mysql数据库之左连接left join 右连接right join 内连接inner join

    最近 xff0c 公司的用户达到了700 43 万 xff0c 意味着数据库已经达到700 43 万 xff0c 聊聊傻傻分不清的连接查询吧 xff01 前提 数据库中一共有三个表 class book phone 而且每个数据库表中都有1
  • VMware虚拟机软件,配置Linux Ubuntu操作系统环境,及安装问题总结大全

    文章目录 1 xff1a 前言2 xff1a 基本认识3 下载环境4 xff1a VM虚拟机的安装5 xff1a ubuntu的下载6 xff1a 把ubuntu安装在VM虚拟机上7 VMware Tools工具8 Source insig
  • linux常用命令(Beginner note)

    命令 ls 列出所有文件及文件夹 ls 路径 xff1a 列出所给路径下的所有文件及文件夹 选项 xff1a xff08 可组合使用 xff0c 也可简写组合形式 xff0c 例 xff1a alh xff0c 无先后顺序 xff09 a
  • 利用JS-SDK微信分享接口调用(后端.NET)

    一直都想研究一下JS SDK微信分享的接口调用 xff0c 由于最近工作需要 xff0c 研究了一下 xff0c 目前只是实现了部分接口的调用 xff1b 其他接口调用也是类似的 xff1b 在开发之前 xff0c 需要提前准备一个微信公众
  • Linux文件查找find

    1 find查找概述 为什么要有文件查找 xff0c 因为很多时候我们可能会忘了某个文件所在的位置 xff0c 此时就需要通过find来查找 find命令可以根据不同的条件来进行查找文件 xff0c 例如 xff1a 文件名称 文件大小 文
  • Linux文件打包与压缩

    1 文件打包与压缩 1 什么是文件压缩 将多个文件或目录合并成为一个特殊的文件 比如 搬家 脑补画面 img 2 为什么要对文件进行压缩 xff1f 当我们在传输大量的文件时 xff0c 通常都会选择将该文件进行压缩 xff0c 然后在进行
  • 集中式版本管理SVN与分布式版本管理Git的区别

    集中式版本控制系统SVN CVS 先说集中式版本控制系统 xff0c 版本库是集中存放在中央服务器的 xff0c 而大家工作的时候 xff0c 用的都是自己的电脑 xff0c 所以要先从中央服务器取得最新的版本 xff0c 然后开始工作 x
  • chatgpt Linux 定时任务 清理rancher pod启动服务的日志文件 脚本

    Linux 定时任务执行命令 假设我们想要每隔X分钟 每隔X天 每天X点执行一个脚本文件 xff0c 可以使用 Linux 自带的 cron 工具来创建定时任务 清理步骤 您可以使用 Linux 自带的 cron 工具来创建定时任务 xff
  • Http协议的几种常见状态码

    在开发好了网站后 xff0c 用户通过URL对资源进行操作 xff0c 服务器端要告诉用户交互的结果 xff0c 比如新增资源是成功还是失败了 一个较好的办法就是遵循HTTP协议 xff0c 使用请求响应的HTTP状态码 xff08 Sta
  • 推荐画UML图以及流程图的在线网站Site

    记得当年学UML课程的时候 xff0c 当你还在为了安装Rose而发愁的时候 xff0c 人家都把作业给交了 xff0c 并且现在大多数UML课程都会让学生使用Rational Rose做画图练习 近来 xff0c 做毕业设计需要提供各种流
  • 浙大PTA平台上的题目题解

    记载一些题目的代码 xff0c 之后想要在b站讲题目 xff0c 到时候会把录的视频上传b站 不是大佬 xff0c 是蒟蒻 xff0c 大佬勿喷 xff0c 仅供参考 xff0c 欢迎大家star xff0c qwq 浙大版 C语言程序设计
  • git 入门教程

    想要将一个项目托管到github xff0c 需要进入项目所在文件夹进行git init命令初始化 Git提交代码的基本流程 xff1a 创建或修改 本地文件使用 git add 命令 xff0c 将创建或修改的文件添加到本地的暂存区 xf
  • 博客搬家

    谢谢大家对我的blog的支持 现在本科毕业 xff0c 准备读研 方向大概是机器学习这一块 又是一个新的开始 我想将我读研学习Python以及机器学习 深度学习 以及数据分析处理 大数据等学习教程放在新的blog上 xff1a blog 欢

随机推荐

  • Python多线程笔记(Python_MultiThread)

    4 MultiThreading 多线程 使用 xff1a a 什么是多线程 xff1f 简单明了 xff0c 让计算机在同一时间内同时运行多个程序 xff0c 并且每个程序的计算互不干扰 xff0c 我们称这样的操作为多线程运算 b ad
  • Python多进程笔记(Python_MultiProcess)

    1 MutiProcessing 多进程 使用 xff1a a 什么是多进程 xff1f 在上面我们使用多线程去分别处理不同的事情 xff0c 看起来 xff0c 多线程处理并不比单线程循环处理的效率看起来那么的高 多进程是在利用我们电脑C
  • Python自带的GUI(Tkinter)教程

    1 Python Tkinter xff08 GUI图形界面 xff09 xff1a a What s Tkinter Tkinter 是什么 xff1f Tkinter是Python自带的一个GUI库 xff0c 他可以将我们在comma
  • Python科学计算包NumPy教程

    在我的Github上有一份代码与教程结合的jupyter Notebook文件 xff0c 大家可以clone下来看一看 下面会用实例的方式给出一些examples xff1a Tutorial教程 官方中文文档 span class to
  • 区块链入门的几个概念

    区块链入门的几个简单概念 1 What s Block Chain 区块链是一门软件技术 xff0c 从本质上来看就像是一个分布式的DataBase xff0c 是一个去中心化 xff0c 分布式技术 由于是分布式的 xff0c 所以区块链
  • Mysql GROUP_CONCAT与CONCAT_WS配合使用单选、多选拼接

    举例1 可以使用IF函数将单选和多选的值分别拼接 xff0c 并在最后的结果中使用CONCAT WS函数将它们合并 xff1a idcolors11223344 551 2 362 3 4 5 我们想要的结果为1 2 3 4 5 下面开始测
  • A Survey on Concept Drift Adaptation Note

    A Survey on Concept Drift Adaptation Abstract Concept drift primarily refers to an online supervised learning scenario w
  • Learning under Concept Drift:A Review

    Learning under Concept Drift A Review Abstract Concept drift describes unforeseeable changes in the underlying distribut
  • Note for Understanding Neural Networks Through Deep Visualization

    Note for Understanding Neural Networks Through Deep Visualization Abstract 近年来 xff0c 在训练大型深度神经网络方面取得了巨大进展 xff0c 其中包括训练卷积
  • ML-Leaks Note

    ML Leaks Model and Data IndependentMembership Inference Attacks and Defenses onMachine Learning Models xff08 机器学习模型上与模型和
  • C++中读取字符串的方式

    这里写自定义目录标题 C 43 43 读取字符串的两种方式1 getline 读取行的输入2 get 读取行的输入 C 43 43 读取字符串的两种方式 1 getline 读取行的输入 getline函数读取整行 xff0c 它使用通过回
  • 二叉树的基本操作

    二叉树 先序和中序确定二叉树 后序以及中序确定二叉树 span class token comment 指针版本 span span class token keyword struct span node span class token
  • 2023秋招面试准备

    2022 秋招资料 算法数据结构 螺旋矩阵问题 螺旋矩阵I 将 1 n m 个数按蛇形方向填入数组中 记录蛇形矩阵偏移量方法 xff0c 四个不同方向右下左上 xff0c 判断什么情况下矩阵遍历完 xff0c 1 要么出界 2 要么走完 s
  • KingbaseES V8R6 维护管理案例之---Kstudio在CentOS 7启动故障

    案例说明 xff1a 在CentOS 7上安装KingbaseES V8R6C006数据库后 xff0c 启动Kstudio图形界面启动失败 xff0c gtk动态库加载失败 xff0c 安装gtk相关动态库后 xff0c 问题解决 适用版
  • python 发送post请求

    背景 浏览器在访问网页时会发送很多http请求 xff08 request xff09 xff0c 服务器返回响应 response xff0c 浏览器拿到响应数据后渲染出来 xff0c 当然我们可以尝试使用python模拟浏览器发送出这些
  • iOS 如何在Label中显示html的文本

    if self messageModel NSString htmlString 61 self messageModel contentText NSAttributedString attrStr1 61 NSAttributedStr
  • Java SpringBoot 集成微信公众号

    微信公众号 申请公众号引入依赖yml配置读取配置文件类WxMaProperties配置文件加载json返回工具类控制层测试号配置以上就完成了哦 xff0c 简单教程Gitee开源地址 申请公众号 注册公众测试号点我 引入依赖 span cl
  • 解决linux python3写入txt文件中换行始终显示"\r\n"

    问题环境 xff1a 在Linux上运行python3脚本远程登录设备 xff0c 远程发送指令 xff0c 并把指令的回显写入文件 出现的问题 xff1a 正常应该显示的换行始终被显示成 r n问题 xff0c 在windows上能够正常
  • linux停止正在执行脚本

    正在执行一个压力测试脚本bench sh xff0c 但是想中途停止进程 xff0c 不再执行了 可以用 ps ef grep 进程名 查出进程 xff0c 然后kill该进程 kill 的用法 xff1a kill xff3b 信号代码
  • 使用fragment实现底部导航菜单栏

    在实现这个功能的过程中 xff0c 走了很多的弯路 xff0c 也花费了较长的时间 xff0c 作为一个新手 xff0c 实现这个功能的过程中 xff0c 分了几个步骤进行尝试 xff0c 首先是会使用fragment xff0c 这个见另