javaandroidpagerslidingtabstrip

How can I remove tabs using PagerSlidingTabStrip?


I have created an application in android using PagerSlidingTabStrip.

I have created tabs using an adapter and fragments.

When I tried to remove a view using

viewPager.removeView(getView());
adapter.notifyDataSetChanged();

The view is hidden but the application shows me an empty view and the tab title persist.


Solution

  • I didn't test this, but it would be something along the lines of:

    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();
    
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }
    
        public void removeFragment(int position) {
            mFragments.remove(position);
            mFragmentTitles.remove(position);
        }
    
        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    
        @Override
        public int getCount() {
            return mFragments.size();
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentsTitles.get(position)
        }
    }