網站首頁 編程語言 正文
Android?BottomNavigationView與Fragment重建與重疊問題解決方法探索_Android
作者:FranzLiszt1847 ? 更新時間: 2023-03-13 編程語言簡介
在BottomNavigationView+多個Fragment框架下,進行Fragment切換時,會導致Fragment重建,也會出現同級Fragment未hide,導致重疊
解決方法
第一步
初始化一個默認需要顯示的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
}
}
第二步
監聽BottomNavigationView切換事件
binding.navView.setOnNavigationItemSelectedListener(listener);
對同級每一個Fragment進行監聽,當進行切換的時候,對其狀態進行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進行隱藏,對要跳轉的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跳轉
當BottomNavigationView中的同級Fragment需要進行跳轉時,可使用EventBus進行跨進程通信實現,然后拿到BottomNavigationView實例進行切換即可,此id為需要跳轉的Fragment 頁面ID
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void OnEvent(ChangeFragBean bean) {
binding.navView.setSelectedItemId(R.id.navigation_dashboard);
}
Activity跳轉到Fragment
同樣使用EventBus,從一個Activity跳轉到BottomNavigationView的某個Fragment時,需要加一個延遲執行,因為Activity可能未銷毀,延遲時間,根據具體手機性能決定,大致在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() {
/**
*要執行的操作
*/
binding.navView.setSelectedItemId(R.id.navigation_member);
}
}, 250);//3秒后執行Runnable中的run方法
}
原文鏈接:https://blog.csdn.net/News53231323/article/details/126015892
相關推薦
- 2023-12-16 IDEA中調用方法時,同步顯示方法的注釋信息
- 2023-06-05 C++頭文件和cpp文件的原理分析_C 語言
- 2022-05-25 C++?自定義單向鏈表?ListNode詳情_C 語言
- 2022-08-15 數據結構之鏈式棧的實現與簡單運用
- 2022-10-17 C++智能指針模板應用詳細介紹_C 語言
- 2022-09-29 一文詳解Golang中net/http包的實現原理_Golang
- 2022-09-19 Python+LyScript實現自定義反匯編_python
- 2021-11-23 Flutter?+?Idea?環境搭建及配置教程_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支