網(wǎng)站首頁 編程語言 正文
Android?BottomNavigationView與Fragment重建與重疊問題解決方法探索_Android
作者:FranzLiszt1847 ? 更新時間: 2023-03-13 編程語言簡介
在BottomNavigationView+多個Fragment框架下,進行Fragment切換時,會導(dǎo)致Fragment重建,也會出現(xiàn)同級Fragment未hide,導(dǎo)致重疊
解決方法
第一步
初始化一個默認需要顯示的Fragment頁面
public void InitFragment(Bundle savedInstanceState) {
//判斷activity是否重建,如果不是,則不需要重新建立fragment.
if (savedInstanceState == null) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
if (mMovie == null) {
mMovie = new HomeFragment();
}
CurrentFragment = mMovie;
fragmentTransaction.replace(R.id.nav_host_fragment_activity_main, mMovie).commit();//fragment parent layout id
}
}
第二步
監(jiān)聽BottomNavigationView切換事件
binding.navView.setOnNavigationItemSelectedListener(listener);
對同級每一個Fragment進行監(jiān)聽,當進行切換的時候,對其狀態(tài)進行show或者hide
private BottomNavigationView.OnNavigationItemSelectedListener listener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
if (mMovie == null) {
mMovie = new HomeFragment();
}
switchContent(CurrentFragment, mMovie);
return true;
case R.id.navigation_dashboard:
if (mExplore == null) {
mExplore = new ExploreFragment();
}
switchContent(CurrentFragment, mExplore);
return true;
case R.id.navigation_notifications:
if (mLibrary == null) {
mLibrary = new LibraryFragment();
}
switchContent(CurrentFragment, mLibrary);
return true;
case R.id.navigation_member:
if (mMember == null) {
mMember = new MemberFragment();
}
switchContent(CurrentFragment, mMember);
return true;
}
return false;
}
};
第三步
此為對原Fragment進行隱藏,對要跳轉(zhuǎn)的Fragment進行show,防止頁面重疊
public void switchContent(Fragment from, Fragment to) {
if (from == null || to == null) return;
if (CurrentFragment != to) {
CurrentFragment = to;
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
if (!to.isAdded()) {
//fragment parent layout id
fragmentTransaction.hide(from).add(R.id.nav_host_fragment_activity_main, to).commit();
} else {
fragmentTransaction.hide(from).show(to).commit();
}
}
}
使用
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private HomeFragment mMovie = null;
private ExploreFragment mExplore = null;
private LibraryFragment mLibrary = null;
private MemberFragment mMember = null;
private Fragment CurrentFragment = null;
InitFragment(savedInstanceState);
binding.navView.setOnNavigationItemSelectedListener(listener);
同級Fragment跳轉(zhuǎn)
當BottomNavigationView中的同級Fragment需要進行跳轉(zhuǎn)時,可使用EventBus進行跨進程通信實現(xiàn),然后拿到BottomNavigationView實例進行切換即可,此id為需要跳轉(zhuǎn)的Fragment 頁面ID
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void OnEvent(ChangeFragBean bean) {
binding.navView.setSelectedItemId(R.id.navigation_dashboard);
}
Activity跳轉(zhuǎn)到Fragment
同樣使用EventBus,從一個Activity跳轉(zhuǎn)到BottomNavigationView的某個Fragment時,需要加一個延遲執(zhí)行,因為Activity可能未銷毀,延遲時間,根據(jù)具體手機性能決定,大致在300-500毫秒即可
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void OnEvent(MermberBean bean) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
/**
*要執(zhí)行的操作
*/
binding.navView.setSelectedItemId(R.id.navigation_member);
}
}, 250);//3秒后執(zhí)行Runnable中的run方法
}
原文鏈接:https://blog.csdn.net/News53231323/article/details/126015892
相關(guān)推薦
- 2023-03-28 python?list與numpy數(shù)組效率對比_python
- 2022-12-28 詳解Flink同步Kafka數(shù)據(jù)到ClickHouse分布式表_數(shù)據(jù)庫其它
- 2022-05-12 tp6 集成swoole
- 2022-06-29 Oracle執(zhí)行Update語句的幾種方式_oracle
- 2022-08-02 使用shell讀取ini文件方法步驟_linux shell
- 2022-08-10 C#對文件名智能排序的算法_C#教程
- 2022-04-25 ASP.NET?Core中Cookie驗證身份用法詳解_實用技巧
- 2022-06-02 C++的智能指針你真的了解嗎_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支