1

I am trying to add and remove tabs the same way the demo does in my code. I have a toggle button that calls arrayAdapter.remove from the remove() method exactly how it is done the demo. When arrayAdapter.remove is called the tab at the requested position is removed correctly but the changes are not reflected by the header for that tab. Also when I try to click on the last tab after removing one tab nothing happens but the tab I want to remove is removed. If this is the logic to rearrange the tabs how would I get rid of this null tab? I know the fragment for the tab is removed because when I try to swipe to it I can't get to it anymore. I am having similar problems with add() to add tabs to the adapter.setTabsToShow() is called when the toggle button is clicked and add() and remove() are the same as in the demo. I have posted my code below.

private void add(boolean before) {  

    int current=mViewPager.getCurrentItem();
    SimplePageDescriptor desc=
            new SimplePageDescriptor(buildTag(arrayAdapter.getCount()),
                    buildTitle(arrayAdapter.getCount()),0);

    if (before) {
        arrayAdapter.insert(desc, current);
    }
    else {
        if (current < arrayAdapter.getCount() - 1) {
            arrayAdapter.insert(desc, current + 1);
        }
        else {
            arrayAdapter.add(desc);
        }
    }
}


private void remove() {
    if (arrayAdapter.getCount() > 1) {
        arrayAdapter.remove(1);

    }
}



public void setTabsToSHow() {


remove();

    }
Tristan
  • 3,530
  • 3
  • 30
  • 39
Blaine
  • 31
  • 6
  • "When arrayAdapter.remove is called the tab at the requested position is removed correctly but the changes are not reflected by the header for that tab" -- I do not know what "the header for that tab" is. "Also when I try to click on the last tab after removing one tab nothing happens but the tab I want to remove is removed" -- I have no idea what you mean by this. "If this is the logic to rearrange the tabs how would I get rid of this null tab?" -- I do not know what a "null tab" is. – CommonsWare Oct 22 '15 at 23:24
  • I have 4 tabs. When I call remove() from one tab to remove another tab the tab I want to be removed is removed successfully. So I can't swipe to it anymore or click anywhere in the list titles for the tabs (which there are still 4 of not 3 like it should be after removing 1) to get to it. So there are still 4 tabs the 3 that I didn't delete and one at the end that does absolutely nothing when I click on it. Not sure what the description for that is but when I try to swipe to it or click on the title to get to the tab nothing happens. – Blaine Oct 22 '15 at 23:44
  • "which there are still 4 of not 3 like it should be after removing" -- then whatever you are using for your tabs is not reacting to changes in the `ViewPager` contents. – CommonsWare Oct 22 '15 at 23:47
  • When you say "whatever you are using for your tabs" what do you mean by that? I was thinking that's what I was using the arrayPagerAdapter for. I guess the main thing i'm trying to figure out is where to look to even started on figuring out why this is happening. Would I need to add code to the remove method in ArrayAdapter? – Blaine Oct 27 '15 at 22:30
  • "what do you mean by that?" -- well, `ViewPager` doesn't have tabs. Something else works with the `ViewPager` to provide tabs. `TabLayout`, `PagerTabStrip`, a seemingly infinite number of third-party libraries, etc. "I guess the main thing i'm trying to figure out is where to look to even started on figuring out why this is happening" -- I would ignore the tabs for the moment and focus on programmatically determining whether the state of the `ArrayPagerAdapter` and `ViewPager` are what you expect after you make changes. Once you're sure they're OK, then figure out why the tabs aren't in sync. – CommonsWare Oct 27 '15 at 22:35

0 Answers0