Referring to invisible fragments in ViewPager

I use a ViewPager with 3 or more fragments displaying and saving a CustomView as a field.

In the process of hosting FragmentActivity, I need to access and set the attributes and fields of CustomView to change the way I display.

The problem arises when I need to access a fragment that has not yet been created, like the third fragment at the beginning of the action (the first fragment is selected by default and only the next fragment is created).

My Acticity:

public class VectorProduct extends FragmentActivity { ViewPager mViewPager; TabsAdapter mTabsAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pager); final ActionBar bar = getSupportActionBar(); bar.setSubtitle(R.string.bt_dashboard_vector_product); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); mViewPager = (ViewPager) findViewById(R.id.viewpager); mTabsAdapter = new TabsAdapter(this, bar, mViewPager); mTabsAdapter.addTab(bar.newTab().setText("Vector 1"), VectorFragment.class); mTabsAdapter.addTab(bar.newTab().setText("Vector 2"), VectorFragment.class); mTabsAdapter.addTab(bar.newTab().setText("Vector 3"), VectorFragment.class); } public static class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener { private final FragmentManager mFragmentManager; private final Context mContext; private final ActionBar mActionBar; private final ViewPager mViewPager; private ArrayList<Class<? extends Fragment>> Fragments; public TabsAdapter(FragmentActivity activity, ActionBar actionBar, ViewPager pager) { super(activity.getSupportFragmentManager()); mFragmentManager = activity.getSupportFragmentManager(); mContext = activity; mActionBar = actionBar; mViewPager = pager; mViewPager.setAdapter(this); mViewPager.setOnPageChangeListener(this); setFragments(new ArrayList<Class<? extends Fragment>>()); } public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss) { mActionBar.addTab(tab.setTabListener(this)); getFragments().add(clss); notifyDataSetChanged(); } @Override public Fragment getItem(int position) { try { return Fragments.get(position).newInstance(); } catch (InstantiationException e) { } catch (IllegalAccessException e) { } return null; } public Fragment findFragment(int position) { String name = "android:switcher:" + mViewPager.getId() + ":" + position; Fragment fragment = mFragmentManager.findFragmentByTag(name); if (fragment == null) { fragment = getItem(position); } return fragment; } } } 
+4
source share
4 answers

If you don't mind the snippets for each page in the ViewPager instance created at any time, then I would change the screen limit on the page in your onCreateView after initializing the ViewPager:

 mViewPager.setOffscreenPageLimit(2); 

Now I put this at 2 since you have 3 tabs. This will be 3 for 4 tabs and so on and so forth. Android API documentation: http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit (int)

Changing the screen limit on the number of tabs minus 1 means that all neighboring fragments for all tabs are loaded. This value is 1 by default, so only the closest tabs are loaded.

Remember that this feature was added recently (I think the support package is v4 release 6) , so make sure you have the updated support package.

+3
source

Try creating an instance of the fragment in the onCreateView () method for a separate fragment class instead of doing it here. See http://android.codeandmagic.org/2011/08/android-dynamic-form-fragment/ for more details.

0
source

The problem is that you create fragments on the fly (as needed), but your application logic assumes that fragments exist at all times (or rather, as long as there is activity). user936414 works because it ensures that fragments exist after the activity is instantiated.

Please note that if you want to show only (bag), you may not want to work with fragments at all. You can directly create views in the ViewPager . This seems especially applicable since you want to access all the contents of the page for all the time. See fooobar.com/questions/236832 / ... for an example with full code.

0
source

You must use nested fragments to embed the fragment inside the parent fragment. Call getChildFragmentManager () in the parent fragment instead of getFragmentManager () to get a working FragmentManager.

0
source

Source: https://habr.com/ru/post/1388660/


All Articles