当前位置:网站首页>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
边栏推荐
- LeetCode·297.二叉树的序列化与反序列化·DFS·BFS
- Alibaba Cloud Jia Zhaohui: Cloud XR platform supports Bizhen Technology to present a virtual concert of national style sci-fi
- Redis上云迁移实践
- 山水的高度
- CodeForces - 834C
- 11 + chrome advanced debugging skills, learn to direct efficiency increases by 666%
- [Advanced Digital IC Verification] Difference and focus analysis between SoC system verification and IP module verification
- 【百度统计】用户行为分析
- H264 码率控制
- iTextSharp 使用详解
猜你喜欢

金山云要飘到哪里?

mSystems | Zhongnong Wang Jie Group Reveals the Mechanisms Affecting Soil "Plastic Interstitial" Microbial Communities

Short read or OOM loading DB. Unrecoverable error, aborting now

es6-promise对象详解

友邦人寿可观测体系设计与落地

BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection 论文笔记

bgp dual plane experiment routing strategy to control traffic

“68道 Redis+168道 MySQL”精品面试题(带解析)

MySQL面试题——MySQL常见查询

【ECCV 2022|百万奖金】PSG大赛:追求“最全面”的场景理解
随机推荐
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama
ArcMAP has a problem of -15 and cannot be accessed [Provide your license server administrator with the following information:Err-15]
代码随想录笔记_动态规划_70爬楼梯
MySQL面试题整理
递归递推之递归的函数
Fragment的show和hide
AtCoder Beginner Contest 077 D - Small Multiple
LeetCode中等题之颠倒字符串中的单词
金山云要飘到哪里?
H264 GOP 扫盲
娄底石油化工实验设计、建设规划概述
YTU 2295: KMP模式匹配 一(串)
Code Casual Recording Notes_Dynamic Programming_70 Climbing Stairs
22家!北京昌平区通报存在食品安全问题餐饮服务企业
Redis 定长队列的探索和实践
Jenkins修改端口号, jenkins容器修改默认端口号
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer Paper Notes
11 + chrome advanced debugging skills, learn to direct efficiency increases by 666%
Twikoo腾讯云函数部署转移到私有部署
ArcMAP出现-15的问题无法访问[Provide your license server administrator with the following information:Err-15]