当前位置:网站首页>Fragment-hide and show
Fragment-hide and show
2022-08-10 13:21:00 【Sharp Pai】
In order to save resources, many apps now use the replace() method to switch fragments, but in this case, if you don't talk about the previous fragments in the Yaru stack, you can't avoid the previous fragments being destroyed., so we use hide() and show() methods to switch fragments.
Achieving the effect
Rendering project address--GitHub - hornhuang/android-SignInSystem: ️ Committed to creating an ultimate sign-in system, technology and beauty can coexist!Also looking forward to your star 🥳~
Here is the best solution without talking nonsense
First, define four members
private Fragment mContent = new Fragment();// Record the current fragment due to replacementprivate Fragment mPerson;private Fragment mRanking;private Fragment mSummary;private FragmentTransaction transaction;
Secondly, in the OnCreate() method, initialize them
mPerson = new Personal();mRanking = new Ranking();mSummary = new Summary();fragmentManager = getSupportFragmentManager();mContent = mRanking;FragmentTransaction transaction = fragmentManager.beginTransaction();transaction.add(R.id.fragment, mContent).commit();navigation.setSelectedItemId(R.id.navigation_home);
Finally, called when the button is clicked
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener= new BottomNavigationView.OnNavigationItemSelectedListener() {@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem item) {switch (item.getItemId()) {case R.id.navigation_home:switchContent(mPerson);return true;case R.id.navigation_dashboard:switchContent(mRanking);return true;case R.id.navigation_ranking:switchContent(mSummary);return true;}return false;}};
private void switchContent(Fragment to) {if (mContent != to) {FragmentTransaction transaction = fragmentManager.beginTransaction();if (!to.isAdded()) { // Determine if it has been added// Hide the current fragment and add the next fragment to ittransaction.hide(mContent).add(R.id.fragment, to).commit();} else {// Hide the current fragment and show the next fragmenttransaction.hide(mContent).show(to).commit();}mContent = to;}}
Complete code address -->
Click to jump -- https://github.com/FishInWater-1999/android-SignInSystem/blob/master/app/src/main/java/com/example/joker/signinsystem/activities/MainActivity.java
—————————————————
Copyright statement: This article is an original article by CSDN blogger "Yuanhao Benhao", following the CC 4.0 BY-SA copyright agreement, please attach a link to the original source and this statement for reprinting.
Original link: https://blog.csdn.net/qq_43377749/article/details/89854480
边栏推荐
- Loudi Center for Disease Control and Prevention Laboratory Design Concept Description
- 想问下大佬们 ,cdc oracle初始化一张300万的表任务运行着后面就这个错 怎么解决哇
- 递归递推之递归的函数
- OTA自动化测试解决方案---整体方案介绍
- Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer Paper Notes
- 在web页面播放rtsp流视频(webrtc)
- 【学习笔记】Redis的持久化
- How to describe multiple paragraphs with different font settings in Open Office XML format
- 啥?他一个人写了个价值100万的软件,却用来开源了!
- Open Office XML 格式里如何描述多段具有不同字体设置的段落
猜你喜欢
Digicert EV证书签名后出现“证书对于请求用法无效”的解决方案
机器学习实战(2)——端到端的机器学习项目
M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
DNS欺骗-教程详解
iTextSharp操作PDF
矩阵键盘&基于51(UcosII)计算器小项目
「企业架构」应用架构概述
Keithley DMM7510 accurate measurement of ultra-low power consumption equipment all kinds of operation mode power consumption
G1和CMS的三色标记法及漏标问题
【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...
随机推荐
金山云要飘到哪里?
kubernetes介绍
11+ chrome高级调试技巧,学会效率直接提升666%
神经网络可视化有3D版本了,美到沦陷!(已开源)
Jenkins修改端口号, jenkins容器修改默认端口号
Real-time data warehouse practice of Baidu user product flow and batch integration
Guidelines for Sending Overseas Mail (2)
Fragment的show和hide
Have you guys encountered this problem?MySQL 2.2 and 2.3-SNAPSHOT are like this, it seems to be
【百度统计】用户行为分析
M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
Requirements for the construction of Loudi stem cell preparation laboratory
【目标检测】小脚本:提取训练集图片与标签并更新索引
Overview of Loudi Petrochemical Experiment Design and Construction Planning
C#中导入其它自定义的命名空间
LeetCode中等题之搜索二维矩阵
交换机的基础知识
R语言实战应用案例:论文篇(一)-特殊柱形图绘制
C#报错 The ‘xmins‘ attribute is not supported in this context
「网络架构」网络代理第一部分: 代理概述