保姆级手把手记录Android studio BottomNavigationView +FrameLayout暴力切换Fragment

本文主要是介绍保姆级手把手记录Android studio BottomNavigationView +FrameLayout暴力切换Fragment,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

开发环境:

效果图:

《《《代码在底部》》》

1,新建项目

2,新建若干Fragment,内容一样,改一下显示出来的Text,名字分别为test1Fragment,test2Fragment,test3Fragment,默认TextView的Text属性分别设置为Fragment1内容,Fragment2内容,Fragment3内容,以示区分

3,拖拽一个FrameLayout

4,拖拽一个BottomNavigationView

5,调整代码

6,新建一个菜单资源给BottomNavigationView使用

7,BottomNavigationView和新建的菜单项关联

8,进一步调整代码

9,MainActivity中的代码

10,红色部分导入类即可解决

代码如下:

1,nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/navigation_home"android:icon="@android:drawable/ic_dialog_map"android:title="Frame1" /><itemandroid:id="@+id/navigation_tools"android:icon="@android:drawable/ic_menu_compass"android:title="Frame2" /><itemandroid:id="@+id/navigation_play"android:icon="@android:drawable/ic_menu_gallery"android:title="Frame3" />
</menu>

2,activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"tools:ignore="ExtraText"><FrameLayoutandroid:id="@+id/fragment_container"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@id/nav_menu"/><com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/nav_menu"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"app:menu="@menu/nav_menu"tools:layout_height="50dp" /></RelativeLayout>

3,fragment_test1.xml  

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".test1Fragment"><!-- TODO: Update blank fragment layout --><TextViewandroid:id="@+id/text1Fragment"android:layout_width="match_parent"android:layout_height="match_parent"android:text="Fragment1内容" /></FrameLayout>

4,MainActivity.java

package com.example.bottomnavigationviewstudy;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;import android.os.Bundle;
import android.view.MenuItem;import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {ArrayList<Fragment> mFragments = new ArrayList<Fragment>();test1Fragment t1f = new test1Fragment();test2Fragment t2f = new test2Fragment();test3Fragment t3f = new test3Fragment();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);BottomNavigationView navView = findViewById(R.id.nav_menu);mFragments.add(t1f);mFragments.add(t2f);mFragments.add(t3f);//设置默认加载视图getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new test1Fragment()).commit();// navView 点击事件navView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem item) {String a = (String) item.getTitle();//判断是哪个item被点击了switch (a) {case "Frame2":getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new test2Fragment()).commit();break;case "Frame3":getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new test3Fragment()).commit();break;default:getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new test1Fragment()).commit();break;}item.setChecked(true);return true;}});}
}

这篇关于保姆级手把手记录Android studio BottomNavigationView +FrameLayout暴力切换Fragment的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/208604

相关文章

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

MySQL Workbench 安装教程(保姆级)

《MySQLWorkbench安装教程(保姆级)》MySQLWorkbench是一款强大的数据库设计和管理工具,本文主要介绍了MySQLWorkbench安装教程,文中通过图文介绍的非常详细,对大... 目录前言:详细步骤:一、检查安装的数据库版本二、在官网下载对应的mysql Workbench版本,要是

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

JDK多版本共存并自由切换的操作指南(本文为JDK8和JDK17)

《JDK多版本共存并自由切换的操作指南(本文为JDK8和JDK17)》本文介绍了如何在Windows系统上配置多版本JDK(以JDK8和JDK17为例),并通过图文结合的方式给大家讲解了详细步骤,具有... 目录第一步 下载安装JDK第二步 配置环境变量第三步 切换JDK版本并验证可能遇到的问题前提:公司常