当前位置:网站首页>Fragment-hide和show
Fragment-hide和show
2022-08-10 12:38:00 【锐湃】
为了节约资源,现在很多 app 采用了 replace() 方法来实现碎片的切换,但是在这种情宽下,如果不讲之前的碎片雅茹栈中,就无法避免之前碎片回随之销毁,所以我们采用 hide() 和 show() 方法来实现碎片的切换。
实现效果
效果图项目地址--GitHub - hornhuang/android-SignInSystem: ️ 致力于打造一款极致签到系统,技术和美是可以并存的哦!同时期待你的 star 🥳~
这里不讲废话直接给出最佳方案
首先,定义四个成员
private Fragment mContent = new Fragment();// 记录下当前碎片 由于替换
private Fragment mPerson;
private Fragment mRanking;
private Fragment mSummary;
private FragmentTransaction transaction;
其次,在 OnCreate() 方法中,对他们进行初始化
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);
最后,在按钮点击时调用
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public 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()) { // 判断是否被add过
// 隐藏当前的fragment,将 下一个fragment 添加进去
transaction.hide(mContent).add(R.id.fragment, to).commit();
} else {
// 隐藏当前的fragment,显示下一个fragment
transaction.hide(mContent).show(to).commit();
}
mContent = to;
}
}
完整代码地址 -->
点击跳转-- https://github.com/FishInWater-1999/android-SignInSystem/blob/master/app/src/main/java/com/example/joker/signinsystem/activities/MainActivity.java
————————————————
版权声明:本文为CSDN博主「圆号本昊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43377749/article/details/89854480
边栏推荐
- 娄底石油化工实验设计、建设规划概述
- NodeJs原理 - Stream(二)
- Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama
- Guo Jingjing's personal chess teaching, the good guy is a robot
- 关于flask中static_folder 和 static_url_path参数理解
- Overview of Loudi Petrochemical Experiment Design and Construction Planning
- C# 当前上下文中不存在InitializeComponent()
- 10 款更先进的开源命令行工具
- 一种能让大型数据聚类快2000倍的方法,真不戳
- 22家!北京昌平区通报存在食品安全问题餐饮服务企业
猜你喜欢
漏洞管理计划的未来趋势
M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
mSystems | 中农汪杰组揭示影响土壤“塑料际”微生物群落的机制
线代 | 秒杀方法与技巧
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer Paper Notes
bgp dual plane experiment routing strategy to control traffic
Proprietary cloud ABC Stack, the real strength!
Solution for "Certificate not valid for requested usage" after Digicert EV certificate signing
Code Casual Recording Notes_Dynamic Programming_70 Climbing Stairs
LeetCode·297.二叉树的序列化与反序列化·DFS·BFS
随机推荐
中科院深圳先进技术院合成所赵国屏院士组2022年招聘启事
H264 码率控制
Code Casual Recording Notes_Dynamic Programming_70 Climbing Stairs
shell:常用小工具(sort、uniq、tr、cut)
Redis 定长队列的探索和实践
C# 当前上下文中不存在InitializeComponent()
想问下大佬们 ,cdc oracle初始化一张300万的表任务运行着后面就这个错 怎么解决哇
实践为主,理论为辅!腾讯大佬MySQL高阶宝典震撼来袭!
Ethernet channel 以太信道
来看Prada大秀吗?在元宇宙里那种!
Loudi Sewage Treatment Plant Laboratory Construction Management
Polygon zkEVM工具——PIL和CIRCOM
[Advanced Digital IC Verification] Difference and focus analysis between SoC system verification and IP module verification
啥?他一个人写了个价值100万的软件,却用来开源了!
10 款更先进的开源命令行工具
可视化服务编排在金融APP中的实践
Loudi Cosmetics Laboratory Construction Planning Concept
Chapter9 : De Novo Molecular Design with Chemical Language Models
接口自动化测试基础篇
MYSQL误删数据恢复