[英]Bottom Navigation & Fragment Recreation
I have a bottom navigation which runs on my Main Activity 我在主活动上运行一个底部导航
private void BottomNavigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
LoadFragment(e.Item.ItemId);
}
void LoadFragment(int id)
{
Android.Support.V4.App.Fragment fragment = null;
switch (id)
{
case Resource.Id.navigation_1:
fragment = Fragment1.NewInstance();
break;
case Resource.Id.navigation_2:
fragment = Fragment2.NewInstance();
break;
case Resource.Id.navigation_3:
fragment = Fragment3.NewInstance();
break;
}
if (fragment == null)
return;
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
In my Fragment1 (which is Resource.Id.navigation_1
) I am refreshing data 在我的Fragment1(即
Resource.Id.navigation_1
)中,我正在刷新数据
public async override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
RefreshItems(false);
}
The issue is, every time I click on navigation_1
for fragment1
, the data gets refreshed. 问题是,每次我单击
fragment1
navigation_1
,数据都会刷新。
I don't want to load the fragment/data again once it's already loaded. 我不想再次加载片段/数据。
Is it possible in Xamarin? Xamarin是否有可能?
You can check if current fragment also executed Here is a function 您可以检查是否也执行了当前片段这是一个函数
public Fragment find(String identifier) {
Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag(identifier);
return fragment;
}
and in BottomNavigationView you must check like this 在BottomNavigationView中,您必须像这样检查
if (find(fragment.getIdentifier()) != null) {
return;
}
I'd this problem sometime ago, however I was developing on native kotlin code, here goes the code I've used to handle this: 我前一段时间会遇到这个问题,但是我正在开发本机kotlin代码,下面是我用来处理此问题的代码:
class Navigation(val supportFragmentManager: FragmentManager,
val filterIcon: ImageView ): BottomNavigationView.OnNavigationItemSelectedListener {
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.menu_bottom_home ->{
changeFragment(HomeFragment(), Constants.FRAG_HOME_FRAGMENT)
filterIcon.visibility = View.VISIBLE
}
R.id.menu_bottom_favorites ->{
changeFragment(FavoritesFragment(), Constants.FRAG_FAVORITES_FRAGMENT)
filterIcon.visibility = View.INVISIBLE
}
R.id.menu_bottom_reservations ->{
changeFragment(ReservationFragment(), Constants.FRAG_RESERVATION_FRAGMENT)
filterIcon.visibility = View.INVISIBLE
}
R.id.menu_bottom_profile ->{
changeFragment(ProfileFragment(), Constants.FRAG_PROFILE_FRAGMENT)
filterIcon.visibility = View.INVISIBLE
}
}
return true
}
fun changeFragment(fragment: Fragment, tag : String){
var contains: Fragment? = null
var current: Fragment? = null
if(supportFragmentManager.fragments != null && supportFragmentManager.fragments.isNotEmpty()) {
contains = supportFragmentManager.fragments.firstOrNull { x -> x.tag == tag }
current = supportFragmentManager.fragments.first { x -> x.isVisible }
}
if(current != null){
supportFragmentManager.beginTransaction()
.hide(current).commit()
}
if(contains != null){
supportFragmentManager.beginTransaction()
.show(contains).commit()
}
else{
supportFragmentManager.beginTransaction()
.add(R.id.fl_fragment_container, fragment, tag)
.addToBackStack(tag)
.commit()
}
}
}
Then you just have to set setOnNavigationItemSelectedListener
with your Navigation class like this: 然后,只需要使用您的Navigation类设置
setOnNavigationItemSelectedListener
,如下所示:
bnv_navigation.setOnNavigationItemSelectedListener(Navigation(supportFragmentManager, iv_filter_bar))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.