Android BottomNavigationView與Fragment重建與重疊問題解決方法探索
簡介
在BottomNavigationView+多個Fragment框架下,進行Fragment切換時,會導致Fragment重建,也會出現(xiàn)同級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
}
}
第二步
監(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方法
}
到此這篇關于Android BottomNavigationView與Fragment重建與重疊問題解決方法探索的文章就介紹到這了,更多相關Android BottomNavigationView與Fragment內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android同步屏障機制sync barrier實例應用詳解
這篇文章主要介紹了Android同步屏障機制sync barrier實例應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-02-02
20.5 語音合成(百度2016年2月29日發(fā)布的tts引擎)
編寫手機App時,有時需要使用文字轉(zhuǎn)語音(Text to Speech)的功能,比如開車時閱讀收到的短信、導航語音提示、界面中比較重要的信息通過語音強調(diào)2016-03-03
android開發(fā)環(huán)境搭建詳解(eclipse + android sdk)
這篇文章主要介紹了android開發(fā)環(huán)境搭建詳解(eclipse + android sdk),需要的朋友可以參考下2014-05-05
擁抱kotlin之如何習慣使用kotlin高階函數(shù)
這篇文章主要給大家介紹了關于擁抱kotlin之如何習慣使用kotlin高階函數(shù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用kotlin具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12
關于Android WebView的loadData方法的注意事項分析
本篇文章是對Android中WebView的loadData方法的注意事項進行了詳細的分析介紹,需要的朋友參考下2013-06-06

