How can I remove tabs using PagerSlidingTabStrip?

395 views Asked by At

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.

2

There are 2 answers

3
tachyonflux On BEST ANSWER

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)
    }
}
0
lacas On

I finally figured it out how to do this

@Override
public int getItemPosition(Object object)
{
    int index = fragments.indexOf(object);
    if (index == -1)
        return POSITION_NONE;
    else
        return index;
}

this is the working code!!!