My problem is I created 8 viewpager in tab layout. Suppose I want 3 view pages, which I need but I want to delete the rest.
This is my code to add tab and I use for loop to delete the remaining tabs but there is the error "Invalid index 6, size is 6" and as a result, it doesn't fulfill my requirement. How do I adjust my for looping function.
BasePagerAdapter adapter = new BasePagerAdapter(getSupportFragmentManager());
Tab1 tab1 = new Tab1();
Tab2 tab2 = new Tab2();
Tab3 tab3 = new Tab3();
Tab4 tab4 = new Tab4();
Tab5 tab5 = new Tab5();
Tab6 tab6 = new Tab6();
Tab7 tab7 = new Tab7();
Tab8 tab8 = new Tab8();
Tab9 tab9 = new Tab9();
adapter.addFragment(tab1, "All");
adapter.addFragment(tab2, "1");
adapter.addFragment(tab3, "2");
adapter.addFragment(tab4, "3");
adapter.addFragment(tab5, "4");
adapter.addFragment(tab6, "5");
adapter.addFragment(tab7, "6");
adapter.addFragment(tab8, "7");
adapter.addFragment(tab9, "8");
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//Creating our pager adapter
//Adding adapter to pager
viewPager.setAdapter(adapter);
int ans =3;
for(int i =8 ; ans<=i ; ans++) {
adapter.removeFragment(ans);
}
int ans=3; //you need only 3
for(int i =8 ; i>ans ; i--) //i for total count. make sure that size
{
adapter.removeFragment(i);//it remove the pager 8,7,6,5,4
}