Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referring to invisble fragments in the ViewPager

I'm using a ViewPager with 3 or more Fragments displaying and storing a CustomView as a field.

During the process of the hosting FragmentActivity I need to access and set attributes and fields of the CustomView in order to alter the way they are displayed.

The problem occurs when I need to access not yet instantiated Fragment like the third Fragment in the beginning of the Activity (the first Fragment is default selected and only the next Fragment is instantiated).

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;
        }
    }
}
like image 665
user1014917 Avatar asked Oct 23 '22 13:10

user1014917


1 Answers

If you don't mind fragments for each page in the ViewPager being instantiated at all times, then I would change the offscreen page limit, in your onCreateView, after initializing the ViewPager:

mViewPager.setOffscreenPageLimit(2);

I put it to 2 now, since you have 3 tabs. It would be 3 for 4 tabs and so on and so forth. Documentation from Android API: http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)

Changing your offscreen limit to the number of tabs minus 1, means that all neighboring fragments for all tabs are being loaded. This value is 1 by default, so only the closest tabs were loaded.

Be aware that this function has been added quite recently (I think support package v4 release 6), so make sure you have your support package up to date.

like image 122
zilinx Avatar answered Oct 27 '22 10:10

zilinx