简体   繁体   English

ViewPager状态在片段中持久存在

[英]ViewPager state persistent in fragment

I have a scenario where I am using bottomNavigation with 3 fragments to load in single Activity. 我有一种情况,我在3个片段中使用bottomNavigation来加载单个Activity。 In fragment two I have a pager with multiple fragments. 在第二个片段中,我有一个带有多个片段的寻呼机。 I wanted to retain the state of fragment two once all respective fragments loaded with views in pager. 一旦所有相应的片段都在分页器中加载了视图,我想保留片段2的状态。

After hectic things I think I have resolved issue.. I have used Singleton design pattern to handle this, 在忙碌的事情之后,我认为我已经解决了问题。.我已经使用Singleton设计模式来处理此问题,

My parent Fragment is a singleton class which will hold fragment list that list will have a fragment to render UI. 我的父Fragment是一个单例类,它将包含片段列表,该列表将具有呈现UI的片段。

Once user will click on bottomNavigation it will load same fragment instance as it already has a list of fragments so once I will insure that it has list I am rendering pager with same list and its same instance of fragment. 一旦用户单击bottomNavigation,它将加载相同的片段实例,因为它已经具有片段列表,因此一旦确保它具有列表,我将使用具有相同列表和相同片段实例的传呼机。

Code: To persiste 代码:坚持

if(listOfFragments!=null&&listOfFragments.size>0)
    binding.tabLayout.setupWithViewPager(notificationBinding.notificationTabsContainer);
    binding.tabLayout.setOnTabSelectedListener(this);
    binding.notificationTabsContainer.setAdapter(pagerAdapter);
    binding.notificationTabsContainer.setOffscreenPageLimit(listOfFragments.size());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM