I create Navigation Drawer with tab view, and thatis ok. But, when I swipe itens on tabView by ViewPager the function getItem don't return what occurs.
My Activity:
setupNavDrawer(4);
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.container);
getLayoutInflater().inflate(R.layout.activity_lte, contentFrameLayout);
String[] titles = {"700Hz", "1800Hz", "2500Hz"};
tabAdapter = new TabAdapter4(getSupportFragmentManager(), this,titles);
mviewPager = (ViewPager) findViewById(R.id.vp);
mviewPager.setAdapter(tabAdapter);
getSupportActionBar().setElevation(0);
tabAdapter = new TabAdapter4(getSupportFragmentManager(), this,titles);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mviewPager);
MyTabAdapter:
public TabAdapter4(FragmentManager fm, Context c, String[] titles) {
super(fm);
this.fm = fm;
this.titles = titles;
this.mContext = c;
}
@Override
public Fragment getItem(int position) {
Fragment frag = null;
switch (position) {
case 0:
//frag = new Lte700Fragment();
frag = Fragment.instantiate(mContext, Lte700Fragment.class.getName());
break;
case 1:
//frag = new Lte18Fragment();
frag = Fragment.instantiate(mContext, Lte18Fragment.class.getName());
break;
case 2:
//frag = new Lte25Fragment();
frag = Fragment.instantiate(mContext, Lte25Fragment.class.getName());
break;
default:
}
Bundle b = new Bundle();
b.putInt("position", position);
frag.setArguments(b);
return frag;
}
@Override
public int getCount() {
return 3;//titles.length;
}
@Override
public CharSequence getPageTitle(int position){
return (titles[position]);
}
Frangments were changed okay, but 'position' in getItem not corresponds at the reality. When I'm first fragment the position return 0 and 1, when I'm second fragment return 2, and return nothing on third fragment.
And this is harmful to my application because I need control AsyncTask by fragment life cycle, because the thread will change when fragment be onPause or onResume.
My doubt is: how return right position for close, for example, fragment 1 and 2 when I'm using 3.