astuetz / PagerSlidingTabStrip

An interactive indicator to navigate between the different pages of a ViewPager
139 stars 44 forks source link

How I remove a tab from PagerSlidingTabStrip? #223

Open rrnogal opened 9 years ago

rrnogal commented 9 years ago

Hello, I have an application that need to remove a tab before make an action. I tried to remove the tab via removeView, this remove me the view but not the tab. The view looks empty but the tab stays there.

Someone can help me?

Sorry by my bad english.

midaslefkowitz commented 9 years ago

What I did was remove the tab title from the underlying data and then called adapter.notifyDataSetChanged().

For example:

This is in my adapter:

private String title1= "title1";
private String title2= "title2";
private String title3= "title3";

private String [] mAllTabTitles = {
    title1,
    title2,
    title3
};

private ArrayList<String> mTabTitles = new ArrayList( Arrays.asList(mAllTabTitles) );

public void removeTab(String title) {
    mTabTitles.remove(title);
    notifyDataSetChanged();
}

@Override
public Fragment getItem(int index) {
    String title = mTabTitles.get(index);

    if (title.equals(title1)) {
        return new Tab1Fragment();
    } else if (title.equals(title2)) {
        return Tab2Fragment();
    } else if (title.equals(title3)) {
        return new Tab3Fragment();
    } 
    return null;
}

Then to remove tab3 I call:

mAdapter.remove(title3);
rrnogal commented 9 years ago

Thank you for you anwer. I will try to do and comment it :)

ajans commented 8 years ago

Did this work for you?

I tried this with enums added to an array-list instead of title array, each enum containing of a title-string-id and a newInstance-method to instantiate the correct fragment.

Problem is, removing or adding an item dynamically works for the viewpager itself, but not for the PagerSlidingTabStrip. It always didn't reflect the changed state and got corrupted. E.g. if I removed an item, the tab was still there, showing the wrong page, or vice versa, if I added an item, I could swipe "out of the tab range" and the PagerSlidingTabStrip crashed my app.

This was the same with calling adaper.notifyDataSetChanged() or tabStrip.notifyDataSetChanged() or even re-initializing the viewpager and tabstrip:

        pager.setAdapter(null);
        pager.setAdapter(this);
        tabStrip.setViewPager(pager);
midaslefkowitz commented 8 years ago

It works great for me. Im sorry, not sure why not working for you...