Reputation: 955
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?
Upvotes: 1
Views: 347
Reputation: 839
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();
Upvotes: 0
Reputation: 955
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.
Upvotes: 0
Reputation: 5791
You can set the number of fragments on the viewpager with offscreenpagelimit.
viewpager.offscreenpagelimit(6);
Upvotes: 5