简体   繁体   English

底部导航和碎片娱乐

[英]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.

相关问题 如何防止在底部导航视图中重新创建片段? - How to prevent Fragment recreation in Bottom Navigation View? 如何在没有娱乐片段的情况下为每个底部导航视图进行单独导航? - How can I make seperate navigation for each bottom nav view without recreation fragment? Android导航架构组件避免了Fragment娱乐 - Android Navigation Architecture component avoid Fragment recreation CollapsingToolbarLayout 使 RecyclerView 在片段视图重新创建后无法滚动直到底部 - CollapsingToolbarLayout makes RecyclerView NOT scrollable until bottom after fragment view recreation 片段恢复导致观察者使用Androidx导航库触发onChanged() - Fragment Recreation causes Observer to trigger onChanged() with Androidx Navigation library 替换底部导航中的片段 - Replacing fragment in bottom navigation 如何使用导航架构组件避免片段重新创建? - How to avoid fragment recreation using navigation architecture component? 底部导航和片段重叠 - Bottom Navigation and Fragment is overlapping 底部导航和片段选项卡布局 - Bottom Navigation and Fragment Tab Layout 将底部导航视图实现到片段中 - Implement Bottom Navigation View into a fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM