简体   繁体   中英

How to create all fragments when application start

I am creating number of fragments programmatically. All of them have different layouts and active fragment's layout is changed after swapping between fragments.

When program is started FragmentPagerAdapter creates first two fragment. If I select last fragment (for example first one to seventh one) FragmentPagerAdapter starts to create seventh fargment and then sixth fragment. At this point I want to select the current fragment using code below.

`(FragmentMasaDesign) getSupportFragmentManager().getFragments().get
                                (myViewPager.getCurrentItem());`

myViewPager.getCurrentItem() returns 6. That is true because I have select 7th fragment and its index is 6. However I have only four fragments that are created these are 1st-2nd-6th-7th. Thus getFragments() method returns me these four fargments and it can't select the current active fragment because of the value that is returned by myViewPager.getCurrentItem()

Is there any way to create all fragments at start of the appliction or how can I get the current fragment before other fragments created?

您可以使用offscreenpagelimit在viewpager上设置片段数。

viewpager.offscreenpagelimit(6);
public class CollectionPagerAdapter extends FragmentPagerAdapter {
    public ArrayList<Departman> lstDepartmanlar;
    public ArrayList<MasaDizayn> lstMasaDizayn;
    ArrayList<String> masaIsimleri;
    public String[] masaPlanIsmi;
    Fragment fragment;
    public Fragment[] fragments = null;

    public static FragmentMasaDesign newInstance(ArrayList<String> masalar, String DepartmanAdi) {
        FragmentMasaDesign myFragment = new FragmentMasaDesign();
        Bundle args = new Bundle();
        args.putStringArrayList("masalar", masalar);
        args.putString("departmanAdi", DepartmanAdi);
        myFragment.setArguments(args);
        return myFragment;
    }

    public CollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        fragment = null;
        if (fragments == null)
            fragments = new Fragment[lstDepartmanlar.size()];
        for (int j = 0; j < lstDepartmanlar.size(); j++) {
            if (i == j) {
                for (Departman departman : lstDepartmanlar) {
                    if (departman.DepartmanEkrani.contentEquals(masaPlanIsmi[i])) {
                        masaIsimleri = new ArrayList<String>();
                        for (MasaDizayn masaDizayn : lstMasaDizayn) {
                            if (masaDizayn.MasaPlanAdi.contentEquals(departman.DepartmanEkrani)) {
                                masaIsimleri.add(masaDizayn.MasaAdi);
                            }
                        }
                    }
                }
                fragment = newInstance(masaIsimleri, lstDepartmanlar.get(i).DepartmanAdi);
            }
        }
        fragments[i] = fragment;
        return fragment;
    }

    @Override
    public int getCount() {
        return lstDepartmanlar.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}

I have created a public fragment array and then stored the created fragment in this array then I have called the current fragment from this array like:

   public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
        fragment = collectionPagerAdapter.fragments[mViewPager.getCurrentItem()];
    }

Also it is possible to find the visible fragment on screen. I will share its code later.

Thanks to all who spend time for my quesntion.

You could do replace one fragment by another fragment like the following

DiplayFragment newFragment = new DiplayFragment();

                                    FragmentTransaction transaction = getSupportFragmentManager()
                                            .beginTransaction();
                                    transaction
                                            .replace(R.id.nav_fl_frag_container, newFragment);
                                    transaction.addToBackStack(null);
                                    transaction.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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