1

我已經創建了滑動選項卡,並添加了一個方法來移除某個位置上給定的選項卡。它工作正常,如果我試圖刪除除當前選項卡以外的任何選項卡。在這種情況下,代碼將引發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

我想我的錯誤。我試圖刪除當前選項卡,但未提供要顯示的替代選項卡。在這種情況下,用戶可能已經刪除了所有的選項卡,這是不可取的。因此,在適配器的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();     
      } 
     } 
    } 
}