如何删除当前操作栏选项卡?

时间:2013-06-26 10:33:14

标签: tabs android-fragments android-actionbar android-viewpager

我创建了滑动标签,并添加了一种方法来删除某个位置的给定标签。如果我尝试删除当前选项卡以外的任何选项卡,它工作正常。在这种情况下,代码抛出IllegalStateException。让我知道我的错误是什么。

以下是我的代码:

public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final String TAG = "TABS_ADAPTER";

final class TabInfo {
    private final Class<?> clss;
    private final Bundle args;

    TabInfo(Class<?> _class, Bundle _args) {
        clss = _class;
        args = _args;
    }
}

public TabsAdapter(FragmentActivity activity, ViewPager pager) {
    super(activity.getSupportFragmentManager());
    mContext = activity;
    mActionBar = activity.getActionBar();
    mViewPager = pager;
    mViewPager.setAdapter(this);
    mViewPager.setOnPageChangeListener(this);
}

public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
    TabInfo info = new TabInfo(clss, args);
    tab.setTag(info);
    tab.setTabListener(this);
    mTabs.add(info);
    mActionBar.addTab(tab);
    notifyDataSetChanged();
}


public int getCount() {
    return mTabs.size();
}

public Fragment getItem(int position) {
    TabInfo info = mTabs.get(position);
    return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}


public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}


public void onPageSelected(int position) {
    mActionBar.setSelectedNavigationItem(position);
}


public void onPageScrollStateChanged(int state) {
}


public void onTabSelected(Tab tab, FragmentTransaction ft) {
    mViewPager.setCurrentItem(tab.getPosition());
    Log.v(TAG, "clicked");
    Object tag = tab.getTag();
    for (int i=0; i<mTabs.size(); i++) {
        if (mTabs.get(i) == tag) {
            mViewPager.setCurrentItem(i);
        }
    }
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

public void onTabReselected(Tab tab, FragmentTransaction ft) {}

public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}

@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {    
    Object tag = tab.getTag();
    for (int i=0; i<mTabs.size(); i++) {
        if (mTabs.get(i) == tag) {
            mViewPager.setCurrentItem(i);
        }
    }       
}

public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}

public void removeTab(ActionBar.Tab tab) {
    mTabs.remove(tab.getTag());
    mActionBar.removeTab(tab);
    //mTabs.remove(mViewPager.getCurrentItem());
    //mActionBar.removeTabAt(mViewPager.getCurrentItem());
    notifyDataSetChanged();
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // TODO Auto-generated method stub
    super.destroyItem(container, position, object);
    FragmentManager manager = ((Fragment) object).getFragmentManager();
    FragmentTransaction trans = manager.beginTransaction();
    trans.remove((Fragment) object);
    trans.commit();
}
}

以下是logcat错误:

   06-26 15:57:00.149: E/AndroidRuntime(17064): FATAL EXCEPTION: main
   06-26 15:57:00.149: E/AndroidRuntime(17064): java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling  PagerAdapter#notifyDataSetChanged! Expected adapter item count: 3, found: 2 Pager id: com.sparktg.weather:id/pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.sparktg.weather.TabsAdapter

1 个答案:

答案 0 :(得分:1)

我弄错了。我试图删除当前选项卡而不提供要显示的备用选项卡。在该模式中,用户可以很好地删除所有选项卡,这是不可取的。因此,在我的适配器的removeTab()方法中,我只是在删除当前/任何其他选项卡之前将当前视图设置为某个默认选项卡。但是,在这种情况下,用户不应该选择删除该默认选项卡。

以下是我改进的方法:

public void removeTab(ArrayList<String> tabNames){
    Log.v(TAG, "Inside removeTab method of TabsAdpater");
    Log.v(TAG, "Current Tab is at position " + mViewPager.getCurrentItem());
    mViewPager.setCurrentItem(0);
    Log.v(TAG, "Current Item is now set at position " + mViewPager.getCurrentItem());
    for (int i=1; i<mTabNames.size(); i++){
        for (int j=0;j<tabNames.size();j++){
            if(tabNames.get(j).equals(mTabList.get(i).getText().toString())){
                Log.v(TAG, "Tabs to be removed are : " + tabNames.get(j));
                mTabs.remove(mTabList.get(i).getTag());
                mActionBar.removeTab(mTabList.get(i));
                mTabNames.remove(mTabList.get(i).getText());
                mTabList.remove(mTabList.get(i));
                notifyDataSetChanged();                 
            }
        }
    }
}